#Assistant function/tool that just returns string values instead of function call

1 messages · Page 1 of 1 (latest)

toxic patioBOT
#

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

#

Yes, it's possible to design an Assistant function that returns string values instead of a classic tool response. However, it's important to note that the Assistant itself doesn't directly call functions or make requests to endpoints. Instead, it simulates these interactions by defining a function schema and feeding them into the Assistant as per the function tool documentation here (https://platform.openai.com/docs/assistants/tools/function-calling).

When the Assistant needs to perform an action, it will call what it thinks is the right function from the pool it was given, and supply input into it as defined by the functions parameters. The state of the run becomes requires_action when this happens. You then get the name of the function that the Assistant has called, and grab the data that it inputted. Then in your program, you call the real function of the same name and pass in the data that the Assistant gave you. This effectively lets your program take over and handle the processing.

If your real function has output that the Assistant is going to need, you can submit it back to the Assistant when you have it. In fact, you have to. So, if there is no output from your real function, you can always just submit an affirmative like a string that says “success”. In your case, you can return the product names as a string from your real function and submit it back to the Assistant.

Remember, the AI model of API does not need to know secrets, nor can it directly access external APIs on the internet. You would write the code that makes the external calls to APIs in your own backend that neither the user nor the AI sees.

#

@topaz prism kapa.ai is still learning and improving, please let us know how it did by reacting below

topaz prism
#

@toxic patio I'm wondering if it's possible to create an Assistant tool, that just returns a list of string values.
Cause I want the Assistant to return all product names of the product-recommendations he made in his response.

#

@toxic patio I'm wondering if it's possible to create an Assistant tool, that just returns a list of string values.
Cause I want the Assistant to return all product names of the product-recommendations he made in his response.

toxic patioBOT
#

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

#

Yes, it's possible to create an Assistant tool that returns a list of string values. The Assistant API allows you to define custom tools that can be used by the Assistant. These tools can return any type of data, including a list of string values.

When defining a tool, you specify the type of data it returns. In your case, you would specify that the tool returns a list of strings. When the Assistant uses this tool, it will return the list of product names.

Here's a simplified example of how you might define such a tool in the Assistant API:

{
  "type": "object",
  "title": "Product Recommendation Tool",
  "properties": {
    "type": {
      "type": "string",
      "description": "The type of tool being defined: `product_recommendation`",
      "enum": ["product_recommendation"]
    },
    "product_names": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "description": "A list of recommended product names."
    }
  },
  "required": ["type", "product_names"]
}

In this example, the product_names property is an array of strings, which will hold the list of product names.

Please note that this is a simplified example and the actual implementation may vary based on your specific use case and the requirements of the Assistant API.

For more details, you can refer to the OpenAI API documentation (https://platform.openai.com/docs/api-reference) and the OpenAI Community forum (https://community.openai.com/).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@topaz prism kapa.ai is still learning and improving, please let us know how it did by reacting below

topaz prism
#

@toxic patio how can i save this list in my own local python-code?

toxic patioBOT
#

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

#

To save the list of product names in your local Python code, you would typically capture the response from the Assistant API call, extract the list from the response, and then save it to a variable. Here's a simplified example:

# Assume `response` is the response from the Assistant API call
response = {
    "type": "product_recommendation",
    "product_names": ["Product 1", "Product 2", "Product 3"]
}

# Extract the list of product names
product_names = response.get('product_names', [])

# Now `product_names` is a list that contains your product names
print(product_names)

In this example, product_names would output: ['Product 1', 'Product 2', 'Product 3'].

Please note that this is a simplified example and the actual implementation may vary based on your specific use case and the structure of the response from the Assistant API.

For more details, you can refer to the OpenAI API documentation (https://platform.openai.com/docs/api-reference) and the OpenAI Community forum (https://community.openai.com/).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@topaz prism kapa.ai is still learning and improving, please let us know how it did by reacting below

topaz prism
#

@toxic patio how do i get the message response of the assistant?

toxic patioBOT
#

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

#

To get the message response from the assistant, you can use the openai.beta.threads.messages.list method. This method retrieves a list of all messages in a thread. You can then filter this list to get only the messages from the assistant.

Here's an example of how you might do this in Node.js, based on the code provided in the extracts (https://community.openai.com/t/479817):

const openai = new OpenAI({
  apiKey: '****', // defaults to process.env["OPENAI_API_KEY"]
});

// Assume you have a thread ID
const threadId = 'your-thread-id';

// Retrieve all messages in the thread
const messages = await openai.beta.threads.messages.list(threadId);

// Filter the messages to get only those from the assistant
const assistantMessages = messages.data.filter(msg => msg.role === 'assistant');

// Now `assistantMessages` is an array that contains all messages from the assistant
console.log(assistantMessages);

In this example, assistantMessages would be an array of message objects from the assistant. Each message object contains various properties, including the message content.

Please note that this is a simplified example and the actual implementation may vary based on your specific use case and the requirements of the OpenAI API.

For more details, you can refer to the OpenAI API documentation (https://platform.openai.com/docs/api-reference) and the OpenAI Community forum (https://community.openai.com/).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@topaz prism kapa.ai is still learning and improving, please let us know how it did by reacting below

topaz prism
#

@toxic patio
{
"type": "object",
"title": "Product Recommendation Tool",
"properties": {
"type": {
"type": "string",
"description": "The type of tool being defined: product_recommendation",
"enum": ["product_recommendation"]
},
"product_names": {
"type": "array",
"items": {
"type": "string"
},
"description": "A list of recommended product names."
}
},
"required": ["type", "product_names"]
}

how can i save the response from this tool to a local variable?

toxic patioBOT
#

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

#

To save the response from this tool to a local variable in Python, you would typically capture the response from the Assistant API call, extract the relevant data from the response, and then save it to a variable. Here's a simplified example:

# Assume `response` is the response from the Assistant API call
response = {
    "type": "product_recommendation",
    "product_names": ["Product 1", "Product 2", "Product 3"]
}

# Extract the list of product names
product_names = response.get('product_names', [])

# Now `product_names` is a list that contains your product names
print(product_names)

In this example, product_names would output: ['Product 1', 'Product 2', 'Product 3'].

Please note that this is a simplified example and the actual implementation may vary based on your specific use case and the structure of the response from the Assistant API.

For more details, you can refer to the OpenAI API documentation (https://platform.openai.com/docs/api-reference) and the OpenAI Community forum (https://community.openai.com/).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@topaz prism kapa.ai is still learning and improving, please let us know how it did by reacting below