ryo-ma / gpt-assistants-api-ui

💬 OpenAI Assistants API chat UI 🛠️ It works easily by setting the ASSISTANT ID 📁 Supports file upload and file download 🏃 Supports Streaming API 🪟 Support to Azure OpenAI
MIT License
192 stars 166 forks source link

Enhancement: how to add function calling with TOOL_MAP? #24

Closed bzbwqz closed 3 months ago

bzbwqz commented 3 months ago

Hello,

Would you please add demo to invoke API, such as Bing Search API? I don't know how to code in tools.py or other part of app.py

I am beginning to learn Assistants API with streamlit and your repo is awesome. Thanks.

luis-amarquez commented 3 months ago

using the following guide from the docs

# tools.py
from azure.cognitiveservices.search.websearch import WebSearchClient
from azure.cognitiveservices.search.websearch.models import SafeSearch
from msrest.authentication import CognitiveServicesCredentials

def bing_search(search_term):
    subscription_key = "YOUR_SUBSCRIPTION_KEY"

    # Instantiate the client and replace with your endpoint.
    client = WebSearchClient(endpoint="YOUR_ENDPOINT", credentials=CognitiveServicesCredentials(subscription_key))

    # Make a request. Replace Yosemite if you'd like.
    web_data = client.web.search(query="Yosemite")

    # do anything additional you want with the data and return a string or json object as a string

# register the tool here
TOOL_MAP = {
    'bing_search': bing_search
}

and then in your assistant settings, if you created it in platform.openai.com, you just add the following function

{
  "name": "bing_search",
  "description": "do a bing search based on a user prompt",
  "parameters": {
    "type": "object",
    "properties": {
      "search_term": {
        "type": "string",
        "description": "The search term to do a bing search with"
      }
    },
    "required": [
      "search_term"
    ]
  }
}

and then run the application and when your assistant uses the function, you will see a chat saying "Function Calling: bing_search" and you will get the result.

Hope that helps

bzbwqz commented 3 months ago

@luis-amarquez

Many thanks. It resolves my problem.