|
| 1 | +from openai import OpenAI, OpenAIError |
| 2 | +from loguru import logger |
| 3 | +import os |
| 4 | + |
| 5 | +ALLOWED_MODELS = [ |
| 6 | + "gpt-3.5-turbo", |
| 7 | + "gpt-3.5-turbo-16k", |
| 8 | + "gpt-4", |
| 9 | + "gpt-4o", |
| 10 | + "gpt-4o-mini", |
| 11 | + "gpt-4-turbo", |
| 12 | +] |
| 13 | + |
| 14 | + |
| 15 | +def topic_summaries( |
| 16 | + topics, |
| 17 | + api_key, |
| 18 | + model="gpt-3.5-turbo-16k", |
| 19 | + content="You are a creative writer.", |
| 20 | + prompt="Provide a 1-2 sentence summary for the following topic:", |
| 21 | + max_tokens=60, |
| 22 | + temperature=0.7, |
| 23 | + top_p=1.0, |
| 24 | + frequency_penalty=0.0, |
| 25 | + presence_penalty=0.0, |
| 26 | +): |
| 27 | + """ |
| 28 | + Generate a 1-2 sentence summary for each topic using OpenAI's GPT model. |
| 29 | +
|
| 30 | + Parameters: |
| 31 | + - topics: List of lists, where each sublist contains words/phrases representing a topic. |
| 32 | + - api_key: API key for OpenAI. |
| 33 | + - model: Model to use (e.g., 'gpt-3.5-turbo-16k'). |
| 34 | + - content: Initial system content. |
| 35 | + - prompt: Prompt for the summary generation. |
| 36 | + - max_tokens: Maximum tokens for each summary. |
| 37 | + - temperature: Creativity level for the model. |
| 38 | + - top_p: Nucleus sampling parameter. |
| 39 | + - frequency_penalty: Penalty for word frequency. |
| 40 | + - presence_penalty: Penalty for word presence. |
| 41 | +
|
| 42 | + Returns: |
| 43 | + - summaries: List of summaries corresponding to each topic. |
| 44 | + """ |
| 45 | + |
| 46 | + # Load the API key from environment if not provided |
| 47 | + if api_key is None: |
| 48 | + api_key = os.getenv("OPENAI_API_KEY") |
| 49 | + |
| 50 | + # Initialize the OpenAI client with your API key |
| 51 | + client = OpenAI(api_key=api_key) |
| 52 | + |
| 53 | + # Validate model |
| 54 | + if model not in ALLOWED_MODELS: |
| 55 | + raise ValueError( |
| 56 | + f"Invalid model. Please choose a valid model from {ALLOWED_MODELS}." |
| 57 | + ) |
| 58 | + |
| 59 | + summaries = [] |
| 60 | + |
| 61 | + for idx, topic in enumerate(topics): |
| 62 | + # Create the prompt for each topic |
| 63 | + topic_prompt = f"{prompt} {', '.join(topic)}." |
| 64 | + |
| 65 | + # Logging the operation |
| 66 | + logger.info(f"--- Generating summary for topic {idx} with model: {model} ---") |
| 67 | + |
| 68 | + try: |
| 69 | + response = client.chat.completions.create( |
| 70 | + model=model, |
| 71 | + messages=[ |
| 72 | + {"role": "system", "content": content}, |
| 73 | + {"role": "user", "content": topic_prompt}, |
| 74 | + ], |
| 75 | + max_tokens=max_tokens, |
| 76 | + temperature=temperature, |
| 77 | + top_p=top_p, |
| 78 | + frequency_penalty=frequency_penalty, |
| 79 | + presence_penalty=presence_penalty, |
| 80 | + ) |
| 81 | + |
| 82 | + # Ensure the response is valid |
| 83 | + if response and len(response.choices) > 0: |
| 84 | + summary = response.choices[0].message.content |
| 85 | + else: |
| 86 | + summary = "No summary generated. Please try again." |
| 87 | + |
| 88 | + summaries.append(summary) |
| 89 | + |
| 90 | + except OpenAIError as e: |
| 91 | + summaries.append(f"An error occurred: {str(e)}") |
| 92 | + |
| 93 | + return summaries |
0 commit comments