Function Call - How to force it to get the parameters only from the user session context, not from other

I used function call, and it return the function_name and parameters, the function_name is ok, but parameters not from the user session context, how to solve this problem?

Welcome to the forum!

Can you post the code you use to create and call the function, also include some example prompts and output so that people can see the issue. Thanks!

Thx @Foxabilo, below is code

import openai
import json

# Example dummy function hard coded to return the same weather
# In production, this could be your backend API or an external API
def get_current_weather(location, unit="fahrenheit"):
    """Get the current weather in a given location"""
    weather_info = {
        "location": location,
        "temperature": "72",
        "unit": unit,
        "forecast": ["sunny", "windy"],
    return json.dumps(weather_info)

# Step 1, send model the user query and what functions it has access to
def run_conversation():
    openai.api_key = 'sk-............................................'
    response = openai.ChatCompletion.create(
        # messages=[{"role": "user", "content": "What's the weather like in Boston?"}],
        messages=[{"role": "user", "content": "How's the weather today?"}],
                "name": "get_current_weather",
                "description": "Get the current weather in a given location",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "The city and state, e.g. San Francisco, CA",
                        "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                    "required": ["location"],


    message = response["choices"][0]["message"]

    # Step 2, check if the model wants to call a function
    if message.get("function_call"):
        function_name = message["function_call"]["name"]
        function_args = json.loads(message["function_call"]["arguments"])

        # Step 3, call the function
        # Note: the JSON response from the model may not be valid JSON
        function_response = get_current_weather(

        # Step 4, send model the info on the function call and function response
        second_response = openai.ChatCompletion.create(
                # {"role": "user", "content": "What is the weather like in boston?"},
                {"role": "user", "content": "How's the weather today?"},
                    "role": "function",
                    "name": function_name,
                    "content": function_response,
        return second_response


below is result

  "choices": [
      "finish_reason": "function_call",
      "index": 0,
      "message": {
        "content": null,
        "function_call": {
          "arguments": "{\n  \"location\": \"San Francisco, CA\"\n}",
          "name": "get_current_weather"
        "role": "assistant"
  "created": 1690640046,
  "id": "chatcmpl-7hezmtfIaBzB0PKCTo9irwLND2xBW",
  "model": "gpt-3.5-turbo-0613",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 19,
    "prompt_tokens": 80,
    "total_tokens": 99
  "choices": [
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "The weather in San Francisco, CA is sunny and windy today with a temperature of 72 degrees.",
        "role": "assistant"
  "created": 1690640047,
  "id": "chatcmpl-7heznMlz7rlj4KrFeyd9fZrrNZXpF",
  "model": "gpt-3.5-turbo-0613",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 20,
    "prompt_tokens": 72,
    "total_tokens": 92

The OpenAI example that you pasted as the only example they initially gave of function calling (and now the same thing but with another function) is not well thought out and primed for hallucination, it’s just a demo. They put an example city in the description to confuse the bot instead of the form of the answer. Then the prompt “How’s the weather today” is naturally going to inspire that hallucination.

One has to prompt and function-description prompt in a way that says that the AI must only submit information that is directly copied from its own conversation history if you have your own function meant to gather information from the user. Then have AI goals that it is supposed to gather and encourage submission of the information if that is your engagement destination.


You could also attempt to add system instructions along the lines of “you must prompt the user for all parameters for a function call” and see how that goes.


Edit- I’m realizing now OP probably meant the current conversation as a “session”, not session as in the web object that holds your users data

Please explain your answers in the context of “function call”, “function parameters”,“conversation history”, “hallucination”, to show that you understand the question being asked about.

Then answer, with some code examples that make it possible for the original poster to understand and implement a functional solution, how might one ensure, in a customer service representative role, that an AI chatbot that has been provided functions like “book appointment”, “have sales contact me” gathers all the personal information required as parameters of the particular function directly and completely from the user before calling the function, with answers fabricated solely by the AI avoided.

Thx all. Adding code like the following would be better, but not absolutely true, in some cases it occasionally happens to get the wrong parameters.

            {"role": "system", "content": "you must prompt the user for all parameters for a function call"},
            {"role": "user", "content": "How's the weather today?"}

I faced a similar issue. In my case, I also had non-mandatory parameters.
So I made a middleware function to verify all parameters for a function. Suppose the parameter’s value is unacceptable instead of making the function call. In that case, I will return a function response from this middleware function with content as a message: “The following XYZ parameters lack value, or please provide value for the following parameter.”
And then, this will be sent back as a function response to chat completion, and a related message will be shown to the user.

How did you achieve this can you share some code.strong text

@pratham can you share some code snippets I didn’t understand how you used middleware function, thanks in advance

@vikaschoudhary102 @adeepakpawar

check this pseudo-code, ig this will help

def stringVerify(argument):
  # check for strings which are empty or just contains a space or weird characters and return false
  # if string is valid then return true

def verifyCall(funcCallGptResponse, funcDesc):
  # funcCallGptResponse is the response we get from gpt if a func call is required
  # funcDesc is the function description (the one we send to gpt) of the function we want to call
  funcParam = list(funcDesc["parameters"]["properties"].keys())
  callFuncArgument = funcCallGptResponse["arguments"] # can use json.load
  funcArgs = [callFuncArgument[item] if item in callFuncArgument else None for item in funcParam]
  funcParamLen = len(funcParam)
  funcRequiredParamLen = len(funcDesc["parameters"]["required"])

  for i in range(funcParamLen):
    # check for missing arguments
    if i < funcRequiredParamLen and funcArgs[i] == None:
      return f"please provide argument for {funcParam[i]} parameter"

    # check for invalid arguments (if arg is invalid and not required, we set it as None)
    paramType = funcDesc["parameters"]["properties"][funcParam[i]]["type"]
    if paramType == "string" and not stringVerify(funcArgs[i]):
      if i < funcRequiredParamLen:
        return f"please provide valid argument for {funcParam[i]} parameter"

  funcArgs = [item for item in funcArgs if not item==None]
  return function(*funcArgs) # using a function handler to make the function call
1 Like