System: Python 3.11 on windows 11. I’m very new to Python AI.
Purpose: I’m grabbing several web pages then asking a question about them like “Summarize these web pages.”
Here’s the code snippet I’m having trouble with.
# Generate summary of content which is a web page.
subname = 'client.chat.completion.create()'
try:
# response = openai.ChatCompletion.create(
client = AsyncOpenAI(api_key = os.environ.get("OPENAI_API_KEY"))
response = client.chat.completions.create(
# api_key = options.apikey,
model=options.llmname, # This is the model name to use.
messages=[
# {"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": f"Please summarize the following content:\n\n{content}"}
],
max_tokens=300 # Adjust summary length
)
except Exception as e:
print(f"{procname} ERROR in {subname}. {e}")
sys.exit(1)
# summary = response.choices[0].message['content'].strip() # Deprecated
# summary = response['choices'][0]['message']['content'].strip() # Wrong
summary = response['choices'][0].message['content'].strip() # Problem here.
return summary
The problem is the last line which says summary = response['choices'][0].message['content'].strip()
. I’ve tried AI several times and it keeps giving me wrong answers. I’m new to AI in Python so there may be details I may not understand.
I’ve tried several things with the response
object with no luck. Here’s the current errors I get in the debugger.
-> summary = response['choices'][0]['message']['content'].strip()
(Pdb) p response['choices']
*** TypeError: 'coroutine' object is not subscriptable
(Pdb) p response.choices
*** AttributeError: 'coroutine' object has no attribute 'choices'
Any idea how to get the summary? The summary should be plain text.
Thank you!