How to convert user input into an action, like calling API

OK, The scenario is like this: I want to convert user inputs into an action, such as calling my own API service. For example, when a user inputs “help me create a post, contents of the post is a meeting at 2 pm this afternoon,” I use a fine-tuned model to obtain specific action parameters, such as the values needed for the API. Then, based on these action parameters, I call the API to create the post. However, when the user’s input is not a command, such as a chat message, I also want to recognize this and call the chat interface of chatgpt. I’m wondering if anyone has tried this scenario before? Not sure fine-tuning is a good way to do that.

Rather than use an LLM, the best way to go about this would be to use a text classifier instead, which can differentiate whether the input is a chat or a api call or something else. This classifier can then futher prompt the system on what to do in each case

1 Like

You could definitely fine-tune a model for entity extraction, assuming you have enough data, and then just some basic Python or other code to call your own API. I made a simple example on how to do this with the base GPT-4 model, and with placeholders for your API (since I don’t have your API docs).

import os
import openai
import requests

# Set OpenAI API key
openai.api_key = os.getenv("OPENAI_API_KEY")

# User input
user_input = "help me create a post, contents of the post is a meeting at 2 pm this afternoon"

# Call GPT-4 for intent recognition and parameter extraction
response = openai.Completion.create(
    prompt=f"Extract task, time, and content from the following user input: \"{user_input}\"",

# Extract information from GPT-4 response
task, time, content = response.choices[0].text.strip().split('\n')

# Replace placeholders with your API's base URL, endpoint, and authorization key
api_url = ''
api_key = 'YOUR_API_KEY'
headers = {'Authorization': f'Bearer {api_key}'}

# Prepare data for API call
data = {
    'task': task,
    'time': time,
    'content': content

# Call your API
api_response =, headers=headers, json=data)

# Check if the request was successful
if api_response.status_code == 200:
    print('API call successful!')
    print('API call failed with status code:', api_response.status_code)

I’m just assuming you’ll need things like the task, time, and content information in separate lines. Of course, this would all be edited according to how your API works. If you want to share your API docs, we can get some working code!

1 Like

Any good benchmark/dataset can be used for evaluating LLM on text2api_calls generation?