Skip to content

Instantly share code, notes, and snippets.

@jcdiv47
Created July 15, 2024 09:45
Show Gist options
  • Save jcdiv47/4e9f6c32fb9ddb8a0fbe39a0e1dd102e to your computer and use it in GitHub Desktop.
Save jcdiv47/4e9f6c32fb9ddb8a0fbe39a0e1dd102e to your computer and use it in GitHub Desktop.
OpenAI Assistant API
import os
import time
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
client = OpenAI()
model = "gpt-4o-2024-05-13"
assistant = client.beta.assistants.create(
name="Text2SQL assistant",
description="An assistant that helps you convert natural language questions into SQL queries.",
instructions="You are a helpful assistant that helps users convert natural language questions into SQL queries.",
model=model,
tools=[{"type": "code_interpreter"}],
)
thread = client.beta.threads.create(
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "上海有多少家星巴克?"
},
],
}
]
)
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id
)
counter = 0
while run.status != "completed":
run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
if counter % 10 == 0:
print(run.status)
counter += 1
time.sleep(2)
print(run)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment