which AI service to use in order to translate a natural human language to an API call?

78233321 20 Reputation points
2024-05-03T10:04:17.5366667+00:00

Hello,

I would like to build a bot that would be able not only to assist in using our system but also do simple tasks e..g.

Add a new product to the system of the type 'pencil' with the name 'the Contoso latest pencil', manufacture 'Contoso'.

The result would be REST API call '[POST] /api/<products-system>/products'

body

{

type: 'pencil',

'name: 'the Contoso latest pencil',

manufacture: 'Contoso'

}

Thanks,

Kiril

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
2,267 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,441 questions
{count} votes

Accepted answer
  1. Charlie Wei 2,845 Reputation points
    2024-05-05T12:41:49.8966667+00:00

    Hello 78233321,

    I'm not sure if this approach meets your requirements.

    The following code will employ function calling to achieve the 'understand' the intent you mentioned. When GPT discerns that a user is inquiring about a product, it will retrieve product information through an API request and respond to related questions.

    For a complete example code, please refer to the Microsoft Learn documentation.

    from openai import AzureOpenAI
    import requests
    
    client = AzureOpenAI(
      azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"), 
      api_key=os.getenv("AZURE_OPENAI_API_KEY"),
      api_version="2024-03-01-preview"
    )
    
    def get_product_info(product_id):
        return requests.get(f"https://<some-product-system>/api/products/product/{product_id}").json()
    
    messages = [{"role": "user", "content": "..."}]
    tools = [
        {
            "type": "function",
            "function": {
                "name": "get_product_info",
                "description": "Obtain information about the product.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "product_id": {
                            "type": "string",
                            "description": "The ID of the product",
                        }
                    },
                    "required": ["product_id"],
                },
            },
        }
    ]
    
    response = client.chat.completions.create(
        model="<REPLACE_WITH_YOUR_MODEL_DEPLOYMENT_NAME>",
        messages=messages,
        tools=tools,
        tool_choice="auto"
    )
    

    est regards,
    Charlie


    If you find my response helpful, please consider accepting this answer and voting yes to support the community. Thank you!

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful