Assistants API and Function Calling Question

I am learning about the tool/function calling aspect of the assistants api. To start with, I thought I wanted to add a web searching capability to an assistant.

is this the right way of defining the tooling?

assistant = client.beta.assistants.create(
    name="AssistantMan",
    instructions="You answer to everything concisely. If you don't know the answer, you search the internet for it.",
    tools=[{"type": "code_interpreter"},
           {
               "type": "function",
               "function": {
                      "name": "searchInternet",
                      "description": "Search the internet for information beyond your knowledge cut-off date",
                      "parameters": {
                          "type": "object",
                          "properties": {
                              "query": {
                                  "type": "string",
                                  "description": "The search query to be used to search the internet."
                              }
                          }
                      }
               }
            }],
    model="gpt-4-turbo"
)

then, assuming I’ve written the method to search the internet which, let’s say looks like this:

def searchInternet(query):
    url = internet_search_api_url
    params = {'q': query}
    response = requests.get(url, params=params)
    return response.json()

then the ones that I am a bit confused are:

  • How to I tell the assistant to call the search internet function?
  • How to process the result of the function call and back to the threads messages again?

Many thanks

nevermind, I think I rougly figured out.

on my thread/run observer I need to do something like this:

def wait_on_run(run, thread):
    while run.status in ["queued", "in_progress", "requires_action"]:
        run = client.beta.threads.runs.retrieve(
            thread_id=thread.id,
            run_id=run.id,
        )
        if run.status == "requires_action":
            print('Processing function call.')

            required_action = run.required_action
            if required_action.type == 'submit_tool_outputs':
                # Access the first tool call
                tool_call = required_action.submit_tool_outputs.tool_calls[0]
                tool_call_id = tool_call.id
                
                # get the query (function param)
                query_str = tool_call.function.arguments
                query_dict = json.loads(query_str)
                query = query_dict.get('query')
                
                #call the external function
                output = searchInternet(query)
                
                # Submitting the tool output after fetching data from the function
                client.beta.threads.runs.submit_tool_outputs(
                    thread_id=thread.id,
                    run_id=run.id,
                    tool_outputs=[{
                        "tool_call_id": tool_call_id,
                        "output": json.dumps(output)
                    }]
                )

        time.sleep(0.2)
    return run

just in case anyone else needs it.