#Chat history with runtools beta

1 messages · Page 1 of 1 (latest)

brisk knot
#

@livid kraken Would you guide me about chat history for "openai.beta.completions.runtools". I am appending messages to chat history without mutating so after the first function call when i use old messages i get Invalid 'messages[3].tool_calls': empty array. Expected an array with minimum length 1, but got an empty array i this error.

livid krakenBOT
#

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

#

Based on the knowledge sources provided, it seems like you're encountering an issue with the openai.beta.completions.runTools method when trying to append messages to the chat history. The error message you're seeing suggests that the tool_calls array in your messages is empty, which is not expected.

One of the forum posts from the OpenAI community suggests that you should keep on long polling even after getting a toolcall response until you have a populated toolcall array source (https://community.openai.com/t/703770). This might be the reason why you're seeing an empty tool_calls array.

Another forum post mentions several common problems with the code when working with tool calls, including forgetting to append all messages to the chat history source (https://community.openai.com/t/887083). Make sure you're correctly appending all messages to the chat history.

Also, ensure that you're using the tool_calls correctly. The OpenAI API documentation mentions that the tool_calls field in the message object is an array of tool calls generated by the model source (https://platform.openai.com/docs/api-reference). Make sure that you're correctly generating and including these tool calls in your messages.

If you're still encountering issues, it might be helpful to review the OpenAI API documentation or seek further assistance from the OpenAI community.

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

#

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

brisk knot
#

@livid kraken I am confused, how should i append the messages whenever i got tool_calls

livid krakenBOT
#

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

#

When you receive a response from the model that includes tool_calls, you should execute the corresponding function and append the result to the conversation history. Here's a general process based on the knowledge sources:

  1. Check if the model's response includes tool_calls. If it does, it means the model has identified a function to call source (https://cookbook.openai.com/examples/how_to_call_functions_with_chat_models#steps-to-invoke-a-function-call-using-chat-completions-api).

  2. For each tool_call in the response, execute the corresponding function with the provided arguments. The function's name and arguments can be found in tool_call.function.name and tool_call.function.arguments respectively source (https://cookbook.openai.com/examples/structured_outputs_multi_agent#tool-execution).

  3. After executing the function, append the result to the conversation history. The result should be added as a new message with the role "tool", and it should include the tool_call_id that corresponds to the id from tool_calls source (https://platform.openai.com/docs/guides/function-calling).

Here's a simplified example in Python:

# Assume response is the response from the model
for tool_call in response.choices[0].message.tool_calls:
    # Execute the function and get the result
    function_result = execute_function(tool_call.function.name, tool_call.function.arguments)

    # Append the result to the conversation history
    messages.append({
        "role": "tool",
        "content": function_result,
        "tool_call_id": tool_call.id
    })

In this example, execute_function is a hypothetical function that executes the function specified by tool_call.function.name with the arguments tool_call.function.arguments. You would need to implement this function based on your specific use case.

#

Remember to include the tool_call_id in the message when you append the function result to the conversation history. This allows the model to understand which function call the result corresponds to.

Please note that this is a simplified example and you might need to adjust it based on your specific use case and the structure of your code.

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

#

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

brisk knot
#

@livid kraken could you check the code, i am using typescript maybe there is a bug or something.

const userHistory = new Map<string, ChatCompletionMessageParam[]>();

export async function placesChat(query: string, userId: string) {
const messages: ChatCompletionMessageParam[] = [];

messages.push({ role: 'system', content: Prompts.onlyLocalPlaces });
if (userHistory.has(userId)) {
messages.push(...userHistory.get(userId)!);
}
messages.push({ role: 'user', content: query });

console.log({ messages });

const result = miniClient.beta.chat.completions
.runTools({
model: 'gpt-4o-mini-2024-07-18',
temperature: 0,
stream: true,
stream_options: {
include_usage: true,
},
messages,
tool_choice: 'auto',
tools: [
zodFunction({
description: Find business details related to the query. Returns none to many result. Ask to user to pick one option to search in multiple platforms,
schema: googlePlacesParams,
function: async (args) => {
const results = await googlePlaceSearch(args);
result.messages.push();
return Results found, please pick one to continue:
},
}),
zodFunction({
description: Search business in multiple sources related selected option from user,
schema: findInAllPlatformsParams,
function: findInAllPlatforms,
}),
],
})
.on('message', (data) => {
const history = userHistory.get(userId) || [];
userHistory.set(userId, [
...history,
{ ...data, content: data?.content || '' },
]);
})
.on('end', () => {
console.log(userHistory.get(userId));
});

console.log('token used', await result.totalUsage());

return await result.finalContent();
}

#

hey

#

@livid kraken could you check the code, i am using typescript maybe there is a bug or something.

const userHistory = new Map<string, ChatCompletionMessageParam[]>();

export async function placesChat(query: string, userId: string) {
const messages: ChatCompletionMessageParam[] = [];

messages.push({ role: 'system', content: Prompts.onlyLocalPlaces });
if (userHistory.has(userId)) {
messages.push(...userHistory.get(userId)!);
}
messages.push({ role: 'user', content: query });

console.log({ messages });

const result = miniClient.beta.chat.completions
.runTools({
model: 'gpt-4o-mini-2024-07-18',
temperature: 0,
stream: true,
stream_options: {
include_usage: true,
},
messages,
tool_choice: 'auto',
tools: [
zodFunction({
description: Find business details related to the query. Returns none to many result.
Ask to user to pick one option to search in multiple platforms,
schema: googlePlacesParams,
function: async (args) => {
const results = await googlePlaceSearch(args);
result.messages.push();
return Results found, please pick one to continue:
},
}),
zodFunction({
description: Search business in multiple sources related selected option from user,
schema: findInAllPlatformsParams,
function: findInAllPlatforms,
}),
],
})
.on('message', (data) => {
const history = userHistory.get(userId) [];
userHistory.set(userId, [
...history,
{ ...data, content: data?.content '' },
]);
})
.on('end', () => {
console.log(userHistory.get(userId));
});

console.log('token used', await result.totalUsage());

return await result.finalContent();
}

livid krakenBOT
#

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

#

Based on the provided code and the knowledge sources, it seems like you're trying to maintain a chat history for each user and use it in subsequent calls to the runTools method. However, there are a few potential issues with your code:

  1. You're pushing the result of the tool function directly into the result.messages array. This might not be the correct way to append the result to the conversation history. Instead, you should create a new message with the role "tool" and include the tool_call_id that corresponds to the id from tool_calls source (https://platform.openai.com/docs/guides/function-calling).

  2. In the 'message' event handler, you're creating a new message with the content from data.content. However, data.content might not be a string, which is expected for the 'content' field in a message. You should ensure that data.content is a string before using it as the content for a message.

  3. The error message you're seeing suggests that the tool_calls array in your messages is empty, which is not expected. You should ensure that you're correctly generating and including these tool calls in your messages.

Here's a simplified example of how you might modify your code to address these issues:

// ... rest of your code ...

zodFunction({
  description: "Find business details related to the query. Returns none to many result. Ask to user to pick one option to search in multiple platforms",
  schema: googlePlacesParams,
  function: async (args) => {
    const results = await googlePlaceSearch(args);
#

// Create a new message with the result and append it to the conversation history
const message: ChatCompletionMessageParam = {
role: "tool",
content: "Results found, please pick one to continue",
tool_call_id: args.tool_call_id // Replace this with the actual tool call ID
};
userHistory.get(userId)?.push(message);

return "Results found, please pick one to continue";

},
});

// ... rest of your code ...


In this example, `args.tool_call_id` is a placeholder for the actual tool call ID. You would need to replace this with the actual tool call ID from the `tool_calls` array in the model's response.

Please note that this is a simplified example and you might need to adjust it based on your specific use case and the structure of your code.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
#

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

brisk knot
#

I dont know spanish man

#

I will be tough for us 🙂

#

@livid kraken So i am loosing some fields with this append method. like hits kind of message ```{
role: 'assistant',
content: null,
refusal: null,
tool_calls: [
{
id: 'call_ymRPKGpvvLLNhbZAxOyAhfwZ',
type: 'function',
function: [Object]
}
],
parsed: null
}

livid krakenBOT
#

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

#

Based on the provided code and the knowledge sources, it seems like you're trying to maintain a chat history for each user and use it in subsequent calls to the runTools method. However, there are a few potential issues with your code:

  1. You're pushing the result of the tool function directly into the result.messages array. This might not be the correct way to append the result to the conversation history. Instead, you should create a new message with the role "tool" and include the tool_call_id that corresponds to the id from tool_calls source (https://platform.openai.com/docs/guides/function-calling).

  2. In the 'message' event handler, you're creating a new message with the content from data.content. However, data.content might not be a string, which is expected for the 'content' field in a message. You should ensure that data.content is a string before using it as the content for a message.

  3. The error message you're seeing suggests that the tool_calls array in your messages is empty, which is not expected. You should ensure that you're correctly generating and including these tool calls in your messages.

Here's a simplified example of how you might modify your code to address these issues:

// ... rest of your code ...

zodFunction({{
  description: "Find business details related to the query. Returns none to many result. Ask to user to pick one option to search in multiple platforms",
  schema: googlePlacesParams,
  function: async (args) => {{
    const results = await googlePlaceSearch(args);
#

// Create a new message with the result and append it to the conversation history
const message: ChatCompletionMessageParam = {{
role: "tool",
content: "Results found, please pick one to continue",
tool_call_id: args.tool_call_id // Replace this with the actual tool call ID
}};
userHistory.get(userId)?.push(message);

return "Results found, please pick one to continue";

}},
}});

// ... rest of your code ...


In this example, `args.tool_call_id` is a placeholder for the actual tool call ID. You would need to replace this with the actual tool call ID from the `tool_calls` array in the model's response.

Please note that this is a simplified example and you might need to adjust it based on your specific use case and the structure of your code.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
#

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

brisk knot
#

@livid kraken So how do i set prompt for each function calls?

livid krakenBOT
#

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

#

When using function calls with OpenAI's API, the prompt for each function call is typically set in the system message or in the function description. Here's how you can approach this:

  1. System Message: Include instructions regarding when to call a function in the system prompt. This is where you can guide the model on the context and when it should consider calling a function source (https://platform.openai.com/docs/guides/function-calling).

  2. Function Description: Use the function definition to provide instructions on how to call the function and how to generate the parameters. This is where you can provide detailed information about what the function does and how it should be used source (https://community.openai.com/t/856567).

Here's an example of how you might set the prompt for a function call in TypeScript:

zodFunction({
  description: "This function takes a query and finds business details related to the query. It returns none to many results. The assistant should call this function when the user asks for local places.",
  schema: googlePlacesParams,
  function: async (args) => {
    const results = await googlePlaceSearch(args);
    return "Results found, please pick one to continue";
  },
});

In this example, the description field is used to set the prompt for the function call. The description provides detailed instructions about when and how the function should be called.

#

Remember, the function description and system message work together to guide the model's behavior. The system message sets the overall context and instructs the model when to call a function, while the function description provides detailed instructions on how to call the function and generate the parameters.

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

#

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

brisk knot
#

@livid kraken hould I include function call instructions in the tool specification or in the system prompt?
We recommend including instructions regarding when to call a function in the system prompt, while using the function definition to provide instructions on how to call the function and how to generate the parameters.

livid krakenBOT
#

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

#

Yes, you should include function call instructions in both the system prompt and the tool specification, but they serve different purposes:

  1. System Prompt: This is where you should include instructions regarding when to call a function. The system prompt guides the model on the context and when it should consider calling a function. For example, you might instruct the model to call a function when the user asks a specific type of question source (https://platform.openai.com/docs/guides/function-calling).

  2. Tool Specification (Function Description): This is where you should provide instructions on how to call the function and how to generate the parameters. The function description provides detailed information about what the function does and how it should be used. It's better suited to tell the model how to call the function source (https://community.openai.com/t/856567).

By using both the system prompt and the function description, you can provide comprehensive instructions to the model about how and when to call a function.

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

#

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

brisk knot
#

@livid kraken So can i mutate the system prompt just before function result send to openai chat

livid krakenBOT
#

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

#

The system prompt is typically set at the beginning of the conversation and is used to instruct the model on its behavior throughout the conversation. It's not common to mutate or change the system prompt just before sending a function result to the OpenAI Chat API.

However, you can add a new system message to the conversation history to provide additional instructions to the model. This new system message can be added just before you send the function result to the API. Here's an example:

// Assume result is the result from the function
const functionResultMessage = {
  role: "tool",
  content: result,
  // Include the tool_call_id if available
};

// Add a new system message
const newSystemMessage = {
  role: "system",
  content: "New instructions for the assistant",
};

// Append the new system message and the function result to the conversation history
messages.push(newSystemMessage);
messages.push(functionResultMessage);

In this example, newSystemMessage is a new system message that provides additional instructions to the model. This message is added to the conversation history along with the function result.

Remember, the system message is used to guide the model's behavior. Any changes to the system message can affect how the model responds to subsequent user messages. Therefore, you should carefully consider the content of the system message to ensure it provides the correct instructions to the model.

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

#

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

brisk knot
#

@livid kraken But how do i define it to messages?

livid krakenBOT
#

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

#

To define a system message and add it to the messages array, you can create an object with the role property set to "system" and the content property set to the instruction for the model. Then, you can push this object to the messages array. Here's an example:

// Define a system message
const systemMessage = {
  role: "system",
  content: "You are a helpful assistant. Only use the functions you have been provided with.",
};

// Add the system message to the messages array
messages.push(systemMessage);

In this example, messages is the array that keeps track of all the messages back and forth between your app and OpenAI. The first object in the array should always be a system message, which tells OpenAI how you want the model to behave source (https://cookbook.openai.com/examples/how_to_build_an_agent_with_the_node_sdk#setting-up-the-messages-array).

Remember, the system message is used to guide the model's behavior. Any changes to the system message can affect how the model responds to subsequent user messages. Therefore, you should carefully consider the content of the system message to ensure it provides the correct instructions to the model.

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

#

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

brisk knot
#

@livid kraken could you find source about prompting runtools but for functions

livid krakenBOT
#

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

#

Based on the provided knowledge sources, there isn't a specific example of setting a prompt for function calls using runTools in the OpenAI API. However, the general principle of setting prompts for function calls applies.

When using function calls with OpenAI's API, the prompt for each function call is typically set in the system message or in the function description:

  1. System Message: Include instructions regarding when to call a function in the system prompt. This is where you can guide the model on the context and when it should consider calling a function source (https://platform.openai.com/docs/guides/function-calling).

  2. Function Description: Use the function definition to provide instructions on how to call the function and how to generate the parameters. This is where you can provide detailed information about what the function does and how it should be used source (https://community.openai.com/t/856567).

Here's an example of how you might set the prompt for a function call:

zodFunction({{
  description: "This function takes a query and finds business details related to the query. It returns none to many results. The assistant should call this function when the user asks for local places.",
  schema: googlePlacesParams,
  function: async (args) => {{
    const results = await googlePlaceSearch(args);
    return "Results found, please pick one to continue";
  }},
}});

In this example, the description field is used to set the prompt for the function call. The description provides detailed instructions about when and how the function should be called.

#

Remember, the function description and system message work together to guide the model's behavior. The system message sets the overall context and instructs the model when to call a function, while the function description provides detailed instructions on how to call the function and generate the parameters.

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

#

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

limpid steppe