Replies: 1 comment
-
I believe you want to extract the text using async def query_openai(text, display, retries=3):
# Load settings from settings.json
settings = load_settings()
max_tokens = settings.get("max_tokens")
temperature = settings.get("temperature")
for i in range(retries):
try:
response = openai.ChatCompletion.create(
model=settings.get("model"),
messages=[
{"role": "system", "content": f"You are a helpful assistant. {settings.get('custom_instructions')}"},
{"role": "user", "content": f"Human: {text}\nAI:"}
],
max_tokens=max_tokens,
temperature=temperature
)
response_content = response['choices'][0]['message']['content'].strip()
if response_content: # Check if the response is not empty
message = response_content
return message
else:
logger.warning(f"Retry {i+1}: Received empty response from OpenAI.")
except Exception as e:
if 'Did you mean to use v1/completions?' in str(e):
# Re-query using v1/completions
prompt = f"You are a helpful assistant. {settings.get('custom_instructions')}\nHuman: {text}"
response = openai.Completion.create(
model=settings.get("model"),
prompt=prompt,
max_tokens=max_tokens,
temperature=temperature
)
response_content = response['choices'][0]['text'].strip()
if response_content:
message = response_content
return message
else:
logger.error(f"Error on try {i+1}: {e}")
if i == retries - 1: # If this was the last retry
error_message = f"Something went wrong after {retries} retries: {e}"
await handle_error(error_message, None, display)
await asyncio.sleep(0.5) # Wait before retrying |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I need help : a fresh eye on the code !
I really feel like I'm going in circles to resolve this error
try:
chat_completion
Extraction de la réponse
Error
Erreur lors de la génération du prompt 1: 'ChatCompletion' object is not subscriptable
Beta Was this translation helpful? Give feedback.
All reactions