Spaces:
Running
Running
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
""" | |
https://platform.openai.com/docs/assistants/overview | |
""" | |
import argparse | |
import time | |
from openai import OpenAI | |
from openai.pagination import SyncCursorPage | |
from openai.types.beta.threads import ThreadMessage | |
from project_settings import environment, project_path | |
def get_args(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
"--openai_api_key", | |
default=environment.get("openai_api_key", default=None, dtype=str), | |
type=str | |
) | |
args = parser.parse_args() | |
return args | |
def main(): | |
args = get_args() | |
client = OpenAI( | |
api_key=args.openai_api_key | |
) | |
assistant = client.beta.assistants.create( | |
name="Math Tutor", | |
instructions="You are a personal math tutor. Write and run code to answer math questions.", | |
tools=[{"type": "code_interpreter"}], | |
model="gpt-4-1106-preview" | |
) | |
print(f"assistant.id: {assistant.id}") | |
thread = client.beta.threads.create() | |
print(f"thread.id: {thread.id}") | |
message = client.beta.threads.messages.create( | |
thread_id=thread.id, | |
role="user", | |
content="I need to solve the equation `3x + 11 = 14`. Can you help me?" | |
) | |
run = client.beta.threads.runs.create( | |
thread_id=thread.id, | |
assistant_id=assistant.id, | |
instructions="Please address the user as Jane Doe. The user has a premium account." | |
) | |
run = client.beta.threads.runs.retrieve( | |
thread_id=thread.id, | |
run_id=run.id | |
) | |
time.sleep(10) | |
messages = client.beta.threads.messages.list( | |
thread_id=thread.id | |
) | |
print(messages) | |
return | |
if __name__ == '__main__': | |
main() | |