Using LLMs with tools

  1. In this tutorial, we will use LLMs to select the correct tools to use. In the following, we recommend using a LLM which has been trained on tool datasets. E.g., consider using:

    For the rest of this section, we will use an LLM using its prompt. Install the LLM of your choice.

  2. Implement the following functions:

  3. Use the OpenAI’s format to describe each of your function (see documentation and example below).

    {
        "name": "get_delivery_date",
        "description": "Get the delivery date for a customer's order. Call this whenever you need to know the delivery date, for example when a customer asks 'Where is my package'",
        "parameters": {
            "type": "object",
            "properties": {
                "order_id": {
                    "type": "string",
                    "description": "The customer's order ID.",
                },
            },
            "required": ["order_id"],
            "additionalProperties": false,
        }
    }
    

    Store all the function descriptions into a list.

  4. Write a function llm_tool which takes as input a query (and possibly the output of the previous function calls) and performs the following steps:

    <aside> 👉🏻

    Expected output for the query "How many r are there in straberrrrrries? How many r are there in rraaaar?" (and no previous function calls)

    ["count_char_occurrences(input_string='straberrrrrries', char='r')", "count_char_occurrences(input_string='rraaaar', char='r')"]

    </aside>

  5. Write a function llm_reasoner which takes as input a query and performs the following steps:

    <aside> 👉🏻

    Expected output

    "How many r are there in straberrrrrries? How many r are there in rraaaar?”

    Based on the function calls and their output:
    
    - For "straberrrrrries", count_char_occurrences() returns 7.
    - For "rraaar", count_char_occurrences() returns 3.
    
    So, the answer is: There are 7 r's in straberrrrrries and 3 r's in rraaaar.
    

    "How much is 123123123 + 4343434?”

    To find the result of 123123123 + 4343434, we can look at the function calls provided and see that they were passed to a calculator function.
    
    According to the output:
    
    {"simple_calculator(operation='add', num1=123123123, num2=4343434)": 127466557}
    
    The calculation results in: **127466557**.
    

    "How many r are there in straberrries? How many a are there in rraaaar? Divide the first value by the second”

    Based on the function calls and their outputs:
    
    * In "straberrries", the character 'r' appears 4 times.
    * In "rraaaar", the character 'a' appears 4 times.
    
    So, dividing the number of 'r's in "straberrries" by the number of 'a's in "rraaaar" gives:
    
    4 (number of 'r's) ÷ 4 (number of 'a's) = 1
    

    "What is the solution for x2-3*x + 21321412412?”**

    To solve the equation x**2 - 3*x + 21321412412, we can use the quadratic formula:
    
    x = (-b ± √(b^2 - 4ac)) / 2a
    
    In this case, a = 1, b = -3, and c = 21321412412.
    
    Plugging these values into the formula, we get:
    
    x = (3 ± √((-3)^2 - 4*1*21321412412)) / 2*1
    x = (3 ± √(9 - 85285649648)) / 2
    x = (3 ± √(-85285649639)) / 2
    
    Since the value under the square root is negative, we know that the solutions will be complex numbers.
    
    Using a calculator or a programming language, we can calculate the solutions:
    
    x ≈ [3/2 - sqrt(85285649639)*I/2, 3/2 + sqrt(85285649639)*I/2]
    
    This matches the output provided in your original function call.
    

    </aside>

  6. Use https://gofastmcp.com/ to create a server with the functionalities in question 2. Link this server with LMStudio (or Ollama). Verify that the server is utilised correctly using the chat.

Using Langchain to generate stories

  1. Using streamlit and langchain , create a simple application that asks the user for a story topic and generates a title for the story.
  2. Upgrade your application so that it includes a chain. The title is first generated and used in another prompt to generate a corresponding story.
  3. Upgrade your application so that it includes a longer chain. The title is first generated, used in another prompt to generate a corresponding story, and a third prompt reviews the story and writes a review.

Performing RAG

A group of researcher would like to annotate a set of research papers to detect the presence of “an Opinion Measurement Tool (OMT) which is a computational system, model, algorithm, or method that automatically processes and analyzes textual data to infer evaluation, belief, or feeling of an individual towards a specific subject”.

For each research paper, they want to extract the answer to the following questions: