Function calling in language models refers to the ability of a model to not only generate text but also interact with external functions or APIs. This capability allows the model to perform specific tasks like retrieving data, performing calculations, or interacting with other systems, enhancing its utility beyond just text generation.

How Function Calling Works

In language models, function calling typically involves the following steps:

  1. Prompting: The model is provided with a prompt that includes an instruction to call a specific function. The prompt might include the function’s name, expected inputs, and sometimes an example of the output.

  2. Function Invocation: The model interprets the prompt and triggers the function call. This can be done through a framework or custom implementation where the model’s output is parsed to identify the function to call.

  3. Execution and Response: The specified function is executed, and the output is either returned to the model or used directly in the application. The model can then continue its operation, potentially using the function’s output in subsequent text generation.

  4. Integration: The integration with functions is often handled by a middleware or framework that connects the model’s output with the appropriate functions, handling inputs and outputs.

LangChain

LangChain is a framework designed to create chains of calls to language models and other utilities. It allows you to build more complex workflows that can include function calls.

Steps to Set Up Function Calling with LangChain:

  1. Install LangChain:

    pip install langchain
  2. Define Functions: Create Python functions that you want to be called by the language model.

    def get_weather(location):
        # Imagine this calls a weather API
        return f"The weather in {location} is sunny."
  3. Set Up the Chain: Use LangChain to set up a sequence where the model’s output triggers the function.

    from langchain.chains import SimpleChain
     
    def model_function(input):
        if "weather" in input:
            location = extract_location(input)
            return get_weather(location)
        return "I can't help with that."
     
    chain = SimpleChain(
        input="What's the weather in New York?",
        model=model_function
    )
     
    result = chain.run()
    print(result)
  4. Run the Chain: Execute the chain to see the model interacting with the function.

Langroid

Langroid is another framework that allows for enhanced interactions with language models, including function calling. It focuses on creating agents that can handle complex tasks by chaining functions and model calls.

Steps to Set Up Function Calling with Langroid:

  1. Install Langroid:

    pip install langroid
  2. Create an Agent: Set up an agent in Langroid that can handle function calls.

    from langroid.agent import Agent
     
    def weather_function(location):
        return f"The weather in {location} is cloudy."
     
    agent = Agent()
    agent.add_function("get_weather", weather_function)
     
    response = agent.query("Get the weather in London")
    print(response)
  3. Define the Interaction: Specify how the agent should interact with the function and handle inputs.

  4. Run the Agent: Deploy the agent to handle function calls as part of the model’s workflow.