CC22
1
When I try to call API in my code, my code is stuck.
I think I did not hit the limits.
Could anyone help me with it?
Thank you so much!
here is my code:
def generate_answer(question, triples,flag):
if flag == ‘front’:
prompt = f"““Question: {triples}\n
{question}\n
answer?\n
“””
else:
prompt = f”"“Question: {question}\n
answer?\n
{triples}\n
“””
response = openai.ChatCompletion.create(
model='gpt-3.5-turbo',
messages=[{"role": "user", "content": prompt}],
max_tokens=100,
temperature=0
)
response = response.choices[0].message['content'].strip()
return response
_j
2
The code is not formatted well in the forum
The response generator is an object; you should output the selected dictionary value to a new variable.
import os, openai
openai.api_key = os.getenv("OPENAI_API_KEY")
def generate_answer(question, triples="", flag=""):
if flag == "front":
user = f"{triples}\n{question}"
else:
user = f"{question}"
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
max_tokens=100,
temperature=0,
messages=[
{"role": "system", "content": "You are a helpful AI assistant"},
{"role": "user", "content": user}
]
)
content = response["choices"][0]["message"]["content"]
return str(content).strip()
print(generate_answer("How big is a banana?"))
There is also no reason with the chat models to label your question as such or ask for an answer. You also should not end with whitespace, which I fixed above.