fabriziosalmi / UglyFeed

Retrieve, aggregate, filter, evaluate, rewrite and serve RSS feeds using Large Language Models for fun, research and learning purposes.
GNU Affero General Public License v3.0
114 stars 3 forks source link

Hope to support third-party OpenAI sites #34

Open jinshuqishi2019 opened 3 weeks ago

jinshuqishi2019 commented 3 weeks ago

Is your feature request related to a problem? Please describe. I use this link openai_api_url: https://myhost.com/v1/chat/completions because it's cheaper, but it doesn't work and still accesses api.openai.com. I hope that third-party OpenAI sites can be supported in the future.

jinshuqishi2019 commented 3 weeks ago
def call_openai_api(api_url, combined_content, model, api_key):
    """Call the OpenAI API with the given parameters."""
    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {api_key}'
    }
    data = {
        "model": model,
        "messages": [
            {"role": "system", "content": "You are a professional assistant, skilled in composing detailed and accurate news articles from multiple sources."},
            {"role": "user", "content": combined_content}
        ]
    }
    try:
        response = requests_retry_session().post(api_url, headers=headers, json=data)
        response.raise_for_status()
        response_json = response.json()
        return response_json['choices'][0]['message']['content']
    except requests.RequestException as e:
        logger.error("OpenAI API request failed: %s", e)
        return None
    except (KeyError, ValueError) as e:
        logger.error("Failed to parse JSON response from OpenAI API: %s", e)
        return None