#How to work with functions

1 messages · Page 1 of 1 (latest)

lapis cosmos
#

@wind sinew I am trying to work with functions and assistants. I give my assistant with api a function which has the JSON format of what I want the assistant to return data as. Give me an example of I would use python to get the function output from the assistant API.

wind sinewBOT
#

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

#

Sorry @lapis cosmos, I am experiencing especially high traffic right now, please try again. :technologist:

lapis cosmos
#

@wind sinew I am trying to work with functions and assistants. I give my assistant with api a function which has the JSON format of what I want the assistant to return data as. Give me an example of I would use python to get the function output from the assistant API.

wind sinewBOT
#

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

#

Sure, I can provide an example based on the information extracted from the OpenAI documentation and example notebooks.

First, you need to define your function when creating an Assistant. Here's an example of how to define a function that gets the current weather:

assistant = client.beta.assistants.create(
 instructions="You are a weather bot. Use the provided functions to answer questions.",
 model="gpt-4-1106-preview",
 tools=[{
 "type": "function",
 "function": {
 "name": "getCurrentWeather",
 "description": "Get the weather in location",
 "parameters": {
 "type": "object",
 "properties": {
 "location": {"type": "string", "description": "The city and state e.g. San Francisco, CA"},
 "unit": {"type": "string", "enum": ["c", "f"]}
 },
 "required": ["location"]
 }
 }
 }]
)

(Source: OpenAI Platform Documentation (https://platform.openai.com/docs/assistants/tools))

Once you've defined your function, you can call it using the Assistant API. Here's an example of how to call a function and get the output:

# Define the function interface in JSON format
function_json = {
 "name": "display_quiz",
 "description": "Displays a quiz to the student, and returns the student's response. A single quiz can have multiple questions.",
 "parameters": {
 "type": "object",
 "properties": {
 "title": {"type": "string"},
 "questions": {
 "type": "array",
 "description": "An array of questions, each with a title and potentially options (if multiple choice).",
 "items": {
 "type": "object",
 "properties": {
 "question_text": {"type": "string"},
 "question_type": {
 "type": "string",
 "enum": ["MULTIPLE_CHOICE", "FREE_RESPONSE"],
 },
 "choices": {"type": "array", "items": {"type": "string"}},
 },
 "required": ["question_text"],
 },
 },
 },
 "required": ["title", "questions"],
 },
}
#

Update the Assistant with the function

assistant = client.beta.assistants.update(
MATH_ASSISTANT_ID,
tools=[
{"type": "code_interpreter"},
{"type": "retrieval"},
{"type": "function", "function": function_json},
],
)

Call the function and get the output

responses = display_quiz(arguments["title"], arguments["questions"])
print("Responses:", responses)

(Source: **OpenAI Cookbook** (<https://cookbook.openai.com/examples/assistants_api_overview_python#functions>))

Please note that the `display_quiz` function is not defined in the extracts. You would need to define this function in your code based on your specific requirements.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------