#Assistants API

1 messages · Page 1 of 1 (latest)

knotty monolith
#

Show me a full code example of how to create an Assistant, send it a message, and retrieve the response using the Assistant API.

stable barnBOT
#

Hi @knotty monolith. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Sure, here's a full example of how to create an Assistant, send it a message, and retrieve the response using the Assistants API. This example is based on the Python SDK.

First, you need to create an Assistant. Here's how you can do it:

from openai import OpenAI

client = OpenAI()

assistant = client.beta.assistants.create(
    name="Math Tutor",
    instructions="You are a personal math tutor. Answer questions briefly, in a sentence or less.",
    model="gpt-4-1106-preview",
)

(Source: OpenAI Cookbook (https://github.com/openai/openai-cookbook/blob/main/examples/Assistants_API_overview_python.ipynb))

Next, you can create a new Thread and add a Message to it. This will hold the state of your conversation, so you don't have to re-send the entire message history each time. Here's how you can do it:

def submit_message(assistant_id, thread, user_message):
    client.beta.threads.messages.create(
        thread_id=thread.id, role="user", content=user_message
    )
    return client.beta.threads.runs.create(
        thread_id=thread.id,
        assistant_id=assistant_id,
    )

def create_thread_and_run(user_input):
    thread = client.beta.threads.create()
    run = submit_message(MATH_ASSISTANT_ID, thread, user_input)
    return thread, run

# Emulating concurrent user requests
thread1, run1 = create_thread_and_run(
    "I need to solve the equation `3x + 11 = 14`. Can you help me?"
)

(Source: OpenAI Cookbook (https://github.com/openai/openai-cookbook/blob/main/examples/Assistants_API_overview_python.ipynb))

Finally, you can retrieve the response from the Assistant:

messages = client.beta.threads.messages.list(thread_id=thread.id)

(Source: OpenAI Cookbook (https://github.com/openai/openai-cookbook/blob/main/examples/Assistants_API_overview_python.ipynb))

#

Please note that you need to replace MATH_ASSISTANT_ID with the actual ID of the Assistant you created.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@knotty monolith kapa.ai is still learning and improving, please let us know how it did by reacting below

knotty monolith
#

@stable barn please show me the full code snippet for an assistant that talks like glados

stable barnBOT
#

Let me check the knowledge base for you. One moment! :books:

#

I'm sorry, but the provided extracts do not contain a specific example of creating an assistant that talks like GLaDOS from the Portal video game series. However, you can create an assistant and instruct it to adopt a specific personality or behavior using the system message. Here's a general example:

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-0613",
    messages=[
        {"role": "system", "content": "You are an AI assistant that speaks like GLaDOS from the Portal series."},
        {"role": "user", "content": "Hello GLaDOS, how are you today?"},
    ],
)
print(response["choices"][0]["message"]["content"])

(Source: OpenAI Cookbook (https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb))

In this example, the system message is used to prime the assistant with the desired behavior. However, please note that the effectiveness of this approach can vary and may require some experimentation and fine-tuning.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@knotty monolith kapa.ai is still learning and improving, please let us know how it did by reacting below

knotty monolith
#

I mean using the Assistant API @stable barn

stable barnBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Sure, here's how you can create an Assistant that talks like GLaDOS using the Assistants API. This example is based on the Python SDK.

First, you need to create an Assistant. Here's how you can do it:

from openai import OpenAI

client = OpenAI()

assistant = client.beta.assistants.create(
    name="GLaDOS",
    instructions="You are an AI assistant that speaks like GLaDOS from the Portal series.",
    model="gpt-4-1106-preview",
)

(Source: OpenAI Cookbook (https://github.com/openai/openai-cookbook/blob/main/examples/Assistants_API_overview_python.ipynb))

Next, you can create a new Thread and add a Message to it. This will hold the state of your conversation, so you don't have to re-send the entire message history each time. Here's how you can do it:

def submit_message(assistant_id, thread, user_message):
    client.beta.threads.messages.create(
        thread_id=thread.id, role="user", content=user_message
    )
    return client.beta.threads.runs.create(
        thread_id=thread.id,
        assistant_id=assistant_id,
    )

def create_thread_and_run(user_input):
    thread = client.beta.threads.create()
    run = submit_message(GLADOS_ASSISTANT_ID, thread, user_input)
    return thread, run

# Emulating concurrent user requests
thread1, run1 = create_thread_and_run(
    "Hello GLaDOS, how are you today?"
)

(Source: OpenAI Cookbook (https://github.com/openai/openai-cookbook/blob/main/examples/Assistants_API_overview_python.ipynb))

Finally, you can retrieve the response from the Assistant:

messages = client.beta.threads.messages.list(thread_id=thread.id)

(Source: OpenAI Cookbook (https://github.com/openai/openai-cookbook/blob/main/examples/Assistants_API_overview_python.ipynb))

#

Please note that you need to replace GLADOS_ASSISTANT_ID with the actual ID of the Assistant you created.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: