#Spiltting functions into a different file for openAI function calling

1 messages · Page 1 of 1 (latest)

flint adder
#

Hello! My code runs when in a single file but when putting the funtion definitions into another file, the LLM can’t seem to use the functions defined. The functions are in the “decoder_functions.py” file. Here is my code: import os
from dotenv import load_dotenv
import json
from openai import OpenAI
import streamlit as st
import decoder_tools
import decoder_functions

Load environment variables

load_dotenv()
deployment_name = "gpt-4o"

client = OpenAI(
api_key=os.getenv("OPENAI_API_KEY"),
base_url=os.getenv("OPENAI_BASE_URL"),
)

def generate_text_with_conversation(messages):
response = client.chat.completions.create(
model=deployment,
messages=messages,
tools=decoder_tools.tools,
tool_choice="auto"
)
return response.choices[0].message

available_tools = {
"decode_base64": decoder_functions.decode_base64,
"decode_base32": decoder_functions.decode_base32,
"decode_url": decoder_functions.decode_url,
}

BASE_SYSTEM_PROMPT = "Base Prompt Here"

messages = []

turn_count = 1
max_turns = 6

while turn_count < max_turns:
response = generate_text_with_conversation(messages)
messages.append(response)
tool_calls = response.tool_calls

if tool_calls:
    for tool_call in tool_calls:
        tool_name = tool_call.function.name
        tool_id = tool_call.id
        arguments = json.loads(tool_call.function.arguments)

        # Call the function
        action_function = available_tools[tool_name]
        result = action_function(**arguments)

        messages.append({
            "role": "tool",
            "content": result,
            "tool_call_id": tool_id,
        })

        continue
else:
    print(response.content)
    break
gritty krakenBOT
#

Hi @flint adder. 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 provided knowledge sources, there's no specific information about splitting function definitions into a separate file when using OpenAI's function calling feature. However, the issue you're experiencing seems to be more related to Python's module import system rather than OpenAI's function calling.

In Python, when you import a module, Python looks for that module in several places (defined in sys.path). If your decoder_functions.py file is not in one of these places, Python won't be able to find it, and you'll get an import error.

Ensure that your decoder_functions.py file is in the same directory as the script you're running. If it's in a different directory, you'll need to modify your PYTHONPATH or use relative imports.

Also, make sure that the functions in decoder_functions.py are defined at the top level of the file, not inside another function or if-statement, and that they're not named the same as the file.

If you're still having trouble, it might be helpful to print out the error message you're getting. This will give you more information about what's going wrong.

Please note that when using function calling with OpenAI, the model doesn't actually execute the functions itself. Instead, it generates parameters that can be used to call your function, which your code can then choose how to handle. Your application is always in full control (source (https://platform.openai.com/docs/guides/function-calling)).

For more information on how to use function calling with OpenAI, you can refer to the OpenAI function calling guide (https://platform.openai.com/docs/guides/function-calling).

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

#

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

flint adder
#

@gritty kraken Can you please check if “ # Call the function
action_function = available_tools[tool_name]
result = action_function(**arguments)
“ is where the issue lies ?

gritty krakenBOT
#

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

#

Based on the provided knowledge sources, there's no specific information about the code snippet you've provided. However, I can provide some general advice on how to debug this part of your code.

The code you've provided is trying to dynamically call a function from the available_tools dictionary using the tool_name as the key. If the tool_name doesn't exist in the available_tools dictionary, you'll get a KeyError.

Here are a few things you can check:

  1. Make sure that the tool_name is in the available_tools dictionary. You can add a print statement before the function call to check this:
print(f"Tool name: {tool_name}")
print(f"Available tools: {available_tools.keys()}")
action_function = available_tools[tool_name]
  1. Ensure that the tool_name matches exactly with the keys in the available_tools dictionary. Python is case-sensitive, so "decode_base64" and "Decode_base64" would be considered different keys.

  2. Check that the function you're trying to call (action_function) can accept the arguments you're passing to it (**arguments). If the function doesn't accept one or more of the arguments, you'll get a TypeError.

If you're still having trouble, it might be helpful to print out the error message you're getting. This will give you more information about what's going wrong.

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

#

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

flint adder
#

@gritty kraken There is no error message, the code prints it’s using the correct tool but then the LLM can not get the results after a few attempts

gritty krakenBOT
#

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

#

Based on the provided knowledge sources, there's no specific information about the issue you're experiencing. However, it seems like the problem might be related to how the results from the function calls are being handled.

In your code, after calling the function and getting the result, you're appending a new message to the messages list with the role "tool", the content as the result, and the tool_call_id. This message is then passed back to the generate_text_with_conversation function in the next iteration of the loop.

If the LLM is not getting the results, it might be because it's not able to correctly interpret the message with the role "tool". Make sure that the format of the message matches what the LLM is expecting.

Also, ensure that the function you're calling is returning the expected results. You can add a print statement after the function call to check this:

result = action_function(**arguments)
print(f"Result: {result}")

If you're still having trouble, it might be helpful to provide more information about what you mean by "the LLM can not get the results". Are you getting an error message? Is the LLM not responding as expected? Any additional details you can provide would be helpful.

For more information on how to use function calling with OpenAI, you can refer to the OpenAI function calling guide (https://platform.openai.com/docs/guides/function-calling).

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

#

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

flint adder
#

@gritty kraken Ok I added the print result statement and it provided this output: “Result: Error decoding Base64: name 'base64' is not defined” My function implementation is as follows: def decode_base64(b64_encoded_string: str):
try:
decoded_bytes = base64.b64decode(b64_encoded_string)
decoded_str = decoded_bytes.decode('utf-8', 'ignore')
return decoded_str
except Exception as e:
return f"Error decoding Base64: {str(e)}"

gritty krakenBOT
#

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

#

The error message "name 'base64' is not defined" suggests that the base64 module has not been imported in your decoder_functions.py file. The base64 module is a built-in Python module for encoding and decoding Base64 strings, and it's used in your decode_base64 function.

To fix this issue, you should add import base64 at the top of your decoder_functions.py file. Here's how your function should look:

import base64

def decode_base64(b64_encoded_string: str):
    try:
        decoded_bytes = base64.b64decode(b64_encoded_string)
        decoded_str = decoded_bytes.decode('utf-8', 'ignore')
        return decoded_str
    except Exception as e:
        return f"Error decoding Base64: {str(e)}"

This will import the base64 module, making the b64decode function available to use in your decode_base64 function.

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

#

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

flint adder
#

@gritty kraken Perfect! It works! Can you please check if my code is following the best practice and is implemented in a professional manner ? Is the looping correct?

gritty krakenBOT
#

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

#

Based on the provided knowledge sources, there's no specific information about best practices for the code you've provided. However, I can provide some general advice on Python best practices and code organization.

  1. Error Handling: Your decode_base64 function includes a try/except block to handle potential errors, which is a good practice. However, catching all exceptions with Exception is generally discouraged as it can hide unexpected errors. If you know the specific exceptions that base64.b64decode can raise, it would be better to catch those.

  2. Function Documentation: It's a good practice to include docstrings at the beginning of your functions to describe what they do, their parameters, and their return values. This makes your code easier to understand and maintain.

  3. Variable Naming: Your variable names are descriptive and follow the standard Python naming conventions (lowercase with words separated by underscores), which is good.

  4. Looping: Your while loop seems to be correctly implemented. It continues until turn_count is less than max_turns, and it breaks if there are no tool calls in the response. However, it's not clear from the provided code where turn_count is incremented. Make sure you're incrementing turn_count within the loop to avoid an infinite loop.

  5. Code Organization: Your code is well-organized and easy to follow. You've separated your code into functions, which improves readability and reusability.

  6. Environment Variables: You're using the dotenv module to load environment variables, which is a good practice for keeping sensitive information like API keys out of your code.

#

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

flint adder
#

@gritty kraken Can you please check the parameters “pattern” and “oneOf” are valid in these function definitions “{
"type": "function",
"function": {
"name": "virustotal_info_with_hash",
"strict": True,
"description": "Get information from VirusTotal with a specific hash",
"parameters": {
"type": "object",
"properties": {
"hash": {
"type": "string",
"description": "The hash to search for in VirusTotal. It should be a 64-character hexadecimal string e.g. cb1553a3c88817e4cc774a5a93f9158f6785bd3815447d04b6c3f4c2c4b21ed7",
"pattern": "^[a-fA-F0-9]{64}$"
}
},
"required": ["hash"],
"additionalProperties": False
}
}
},
{
"type": "function",
"function": {
"name": "virustotal_info_with_ip",
"strict": True,
"description": "Get information from VirusTotal with a specific IP",
"parameters": {
"type": "object",
"properties": {
"ip": {
"type": "string",
"description": "The IP to search for in VirusTotal. It should be a valid IPv4 or IPv6 address e.g. 192.168.1.1 or 2001:0db8:85a3:0000:0000:8a2e:0370:7334",
"oneOf": [
{ "format": "ipv4" },
{ "format": "ipv6" }
]
}
},
"required": ["ip"],
"additionalProperties": False
}
}” if they aren’t, please let me know how I can incorporate them

gritty krakenBOT
#

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

#

Based on the information provided in the knowledge sources, the pattern and oneOf keywords are not currently supported by OpenAI's structured outputs feature. This is mentioned in the OpenAI Structured Outputs Guide (https://platform.openai.com/docs/guides/structured-outputs) under the section "Some type-specific keywords are not yet supported".

Here are the unsupported keywords:

  • For strings: minLength, maxLength, pattern, format
  • For numbers: minimum, maximum, multipleOf
  • For objects: patternProperties, unevaluatedProperties, propertyNames, minProperties, maxProperties
  • For arrays: unevaluatedItems, contains, minContains, maxContains, minItems, maxItems, uniqueItems

If you turn on Structured Outputs by supplying strict: true and call the API with an unsupported JSON Schema, you will receive an error.

Therefore, you won't be able to use the pattern keyword for the hash parameter in the virustotal_info_with_hash function, and the oneOf keyword for the ip parameter in the virustotal_info_with_ip function.

As an alternative, you could handle the validation of these parameters in your function implementation. For example, you could check if the hash is a 64-character hexadecimal string and if the ip is a valid IPv4 or IPv6 address in the respective functions, and return an error if they're not.

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

#

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

soft crypt
#

@flint adder , I scanned through most of the conversation. I suspect the problem is that when you ask the ai to run some code, it is being run on the server, and your separate file is not in the path on that server. I do not have any suggestion on how to fix that though.

flint adder
#

I see thank u! 🙏 @soft crypt

flint adder
#

@gritty kraken Please go through the documentation and find the best way to implement tool calling where the LLM calls all the tool call back to back if needed and the outputs the final answer. Currently I’m using a loop but it sometimes doesn’t conduct all the tool calls and prints the ouput.

gritty krakenBOT
#

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

#

Based on the provided knowledge sources, the best way to implement tool calling where the Language Learning Model (LLM) calls all the tools back-to-back and outputs the final answer is to use a loop that iterates over the tool calls in the response from the LLM. This is demonstrated in the OpenAI Cookbook example (https://cookbook.openai.com/examples/structured_outputs_multi_agent#tool-execution) and the OpenAI Function Calling Guide (https://platform.openai.com/docs/guides/function-calling).

Here's a simplified version of the loop:

while turn_count < max_turns:
    response = generate_text_with_conversation(messages)
    messages.append(response)
    tool_calls = response.tool_calls

    if tool_calls:
        for tool_call in tool_calls:
            tool_name = tool_call.function.name
            tool_id = tool_call.id
            arguments = json.loads(tool_call.function.arguments)

            # Call the function
            action_function = available_tools[tool_name]
            result = action_function(**arguments)

            messages.append({
                "role": "tool",
                "content": result,
                "tool_call_id": tool_id,
            })

            continue
    else:
        print(response.content)
        break

In this loop, the LLM generates a response and any tool calls. For each tool call, the corresponding function is called, and the result is added to the messages list. The loop continues until there are no more tool calls or the maximum number of turns is reached.

If your loop sometimes doesn't conduct all the tool calls, it might be because the LLM is not generating the expected tool calls. You could add print statements in your loop to check the content of the tool_calls and messages variables at each iteration.

#

If you're still having trouble, it might be helpful to provide more information about what you mean by "it sometimes doesn’t conduct all the tool calls and prints the output". Are you getting an error message? Is the LLM not responding as expected? Any additional details you can provide would be helpful.

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

#

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

flint adder
#

@gritty kraken Please let me know what structured outputs are and do they mean the LLM will return the output in a predefined structured manner?

gritty krakenBOT
#

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

#

Structured Outputs is a feature introduced by OpenAI in August 2024. It ensures that the arguments generated by the model exactly match the provided JSON Schema, enhancing reliability and reducing errors. This feature can be enabled by setting "strict": true in the JSON Schema (source (https://platform.openai.com/docs/guides/function-calling)).

Structured Outputs is the evolution of JSON mode. While both ensure valid JSON is produced, only Structured Outputs ensure schema adherence. Both Structured Outputs and JSON mode are supported in the Chat Completions API, Assistants API, Fine-tuning API, and Batch API. However, Structured Outputs with response_format: {type: "json_schema", ...} is only supported with certain model snapshots (source (https://platform.openai.com/docs/guides/structured-outputs/handling-user-generated-input)).

So, in essence, yes, Structured Outputs mean that the LLM will return the output in a predefined structured manner that adheres to the provided JSON Schema. This can help ensure that the output from the model is in the expected format and can be easily processed by your application.

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

#

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

flint adder
#

@gritty kraken Can you provide an example of how I can achieve a structured output ? Use the example of using tool calling and getting data from the virustotal api in a table format showing all data related to the hash

gritty krakenBOT
#

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

#

Based on the provided knowledge sources, there's no specific example of using tool calling to get data from the VirusTotal API in a table format. However, I can provide a general example of how you might structure your tool and function definitions to achieve a structured output.

First, you would define your tool in the tools parameter of the chat.completions.create method:

tools = [
    {
        "type": "function",
        "function": {
            "name": "virustotal_info_with_hash",
            "strict": True,
            "description": "Get information from VirusTotal with a specific hash",
            "parameters": {
                "type": "object",
                "properties": {
                    "hash": {
                        "type": "string",
                        "description": "The hash to search for in VirusTotal."
                    }
                },
                "required": ["hash"],
                "additionalProperties": False
            }
        }
    }
]

Then, you would define your virustotal_info_with_hash function to return a structured output. For example, if you want to return the data in a table format, you might return a list of dictionaries, where each dictionary represents a row in the table:

def virustotal_info_with_hash(hash: str):
    # Call the VirusTotal API and get the data
    data = call_virustotal_api(hash)

    # Convert the data to a table format
    table = convert_data_to_table(data)

    return table

In this example, call_virustotal_api and convert_data_to_table are placeholder functions that you would need to implement. call_virustotal_api should call the VirusTotal API and get the data for the given hash, and convert_data_to_table should convert the data to a table format.

#

Please note that this is a general example and might need to be adjusted based on the specific requirements of your application and the data returned by the VirusTotal API.

For more information on how to use Structured Outputs with function calling, you can refer to the OpenAI Function Calling Guide (https://platform.openai.com/docs/guides/function-calling).

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

#

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

flint adder
#

@gritty kraken You didn’t enable structured output in this example ?

gritty krakenBOT
#

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

#

You're correct, I apologize for the oversight. To enable Structured Outputs, you would need to set strict: true in the response_format parameter of the chat.completions.create method. Here's how you might do it:

response = openai.ChatCompletion.create(
  model="gpt-4o-2024-08-06",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Get information from VirusTotal with hash 'abcd'."},
    ],
  tools=tools,
  response_format={
    "type": "json_schema",
    "json_schema": {
      "strict": True,
      "schema": {
        "type": "object",
        "properties": {
          "hash_info": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "property_name": {"type": "string"},
                "property_value": {"type": "string"}
              },
              "required": ["property_name", "property_value"]
            }
          }
        },
        "required": ["hash_info"]
      }
    }
  }
)

In this example, the response_format parameter is set to a JSON Schema that specifies the structure of the output. The output is expected to be an object with a hash_info property, which is an array of objects. Each object in the array represents a row in the table and has two properties: property_name and property_value.

Please note that this is a general example and might need to be adjusted based on the specific requirements of your application and the data returned by the VirusTotal API.

#

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

flint adder
#

@gritty kraken So everytime I call this function, it will return a table formatted answer, but what if I ask “Get me the date this hash was created ?” Will it return the answer normally or will it return a table formatted answer ?

gritty krakenBOT
#

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

#

If you have enabled Structured Outputs and set a specific schema for the response, the model will always return the output that adheres to that schema. In the example provided, the schema specifies that the output should be an array of objects (representing a table), so the model will always return the output in that format, regardless of the specific question asked.

If you ask "Get me the date this hash was created?", the model will still return the output in a table format, as specified by the schema. The table might contain a single row with the property name "Date Created" and the property value as the creation date.

If you want the model to return different types of outputs based on the question, you would need to adjust the schema accordingly. However, please note that the current version of Structured Outputs does not support conditional schemas or schemas that allow multiple types of outputs.

For more flexibility in the output format, you might consider not using Structured Outputs, or using it in combination with regular (unstructured) outputs. You could have the model generate both a structured output and a regular output, and then choose which one to use based on the specific question or context.

For more information on how to use Structured Outputs with function calling, you can refer to the OpenAI Function Calling Guide (https://platform.openai.com/docs/guides/function-calling) and the OpenAI Cookbook example (https://cookbook.openai.com/examples/structured_outputs_intro).

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

#

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

flint adder
#

@gritty kraken Ok how can I implement using both, structured and unstructured output and then choose which one to use based on the specific question or context ?

gritty krakenBOT
#

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

#

Based on the provided knowledge sources, there isn't a direct way to implement both structured and unstructured outputs simultaneously in a single API call. The OpenAI API currently supports either structured outputs or unstructured outputs in a single call, but not both.

However, you could potentially implement a workaround by making two separate API calls: one with structured outputs enabled and one with structured outputs disabled. Then, based on the specific question or context, you could choose which output to use.

Here's a simplified example:

# Define your tool
tools = [
    {
        "type": "function",
        "function": {
            "name": "virustotal_info_with_hash",
            "strict": True,
            "description": "Get information from VirusTotal with a specific hash",
            "parameters": {
                "type": "object",
                "properties": {
                    "hash": {
                        "type": "string",
                        "description": "The hash to search for in VirusTotal."
                    }
                },
                "required": ["hash"],
                "additionalProperties": False
            }
        }
    }
]

# Define your message
message = {
    "role": "user",
    "content": "Get me the date this hash was created."
}
#

Make an API call with structured outputs enabled

response_with_structured_outputs = openai.ChatCompletion.create(
model="gpt-4o-2024-08-06",
messages=[message],
tools=tools,
response_format={
"type": "json_schema",
"json_schema": {
"strict": True,
"schema": {
"type": "object",
"properties": {
"hash_info": {
"type": "array",
"items": {
"type": "object",
"properties": {
"property_name": {"type": "string"},
"property_value": {"type": "string"}
},
"required": ["property_name", "property_value"]
}
}
},
"required": ["hash_info"]
}
}
}
)

Make an API call with structured outputs disabled

response_without_structured_outputs = openai.ChatCompletion.create(
model="gpt-4o-2024-08-06",
messages=[message],
tools=tools
)

Choose which output to use based on the specific question or context

if specific_condition:
output = response_with_structured_outputs
else:
output = response_without_structured_outputs


In this example, `specific_condition` is a placeholder for the condition that determines which output to use. You would need to replace it with the actual condition based on your specific requirements.

Please note that making two separate API calls for each question will double the number of API calls you make, which could increase your costs and potentially slow down your application.
#

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

flint adder
#

@gritty kraken Can you provide a guideline for building an LLM app using openai API that provides output based on the documents provided ? Please provide up-to-date information

gritty krakenBOT
#

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

#

Sure, here's a general guideline on how you can build an application using OpenAI's API that generates responses based on a set of documents:

  1. Prepare Your Documents: The first step is to prepare your documents. You'll need to convert your documents into a format that can be used for training the model. This typically involves converting the documents into a JSONL (JSON Lines) format, where each line is a separate JSON object representing a document.

  2. Upload Your Documents: Once your documents are prepared and formatted, you'll need to upload them using the Files API. The OpenAI platform supports file uploads for the purpose of fine-tuning (source (https://community.openai.com/t/868675)).

  3. Fine-Tune the Model: After uploading your documents, you can use them to fine-tune the model. Fine-tuning involves training the model on your documents so that it can generate responses based on the information in the documents.

  4. Create a Chat Interface: Once the model is fine-tuned, you can use the Chat Completions API to create a chat interface. The chat interface allows users to interact with the model by asking questions or making requests, and the model generates responses based on the fine-tuned documents (source (https://platform.openai.com/docs/guides/text-generation)).

  5. Handle User Queries: When a user asks a question, you can use the Chat Completions API to generate a response from the model. The model will generate a response based on the fine-tuned documents. If the user asks a question that is not covered by the documents, you can program the model to give a negative response (source (https://community.openai.com/t/868675)).

#
  1. Implement Moderation: To ensure that the content generated by the model is appropriate and safe, you can use OpenAI's Moderation API. The Moderation API allows you to filter user inputs and scrutinize model-generated responses for appropriateness (source (https://cookbook.openai.com/examples/how_to_use_moderation#conclusion)).

Please note that this is a general guideline and might need to be adjusted based on the specific requirements of your application. For more detailed information, you can refer to the OpenAI API documentation (https://platform.openai.com/docs/).

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

#

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

flint adder
#

@gritty kraken How do I implement document processing using the openAI API ? Can you please provide a sample code ?

gritty krakenBOT
#

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

#

Based on the provided knowledge sources, there's no specific information about document processing using the OpenAI API. However, OpenAI's GPT-3 model can be used to process and analyze documents in various ways, such as summarizing text, extracting key points, translating text, and more.

Here's a general example of how you might use the OpenAI API to process a document:

import openai

openai.api_key = 'your-api-key'

response = openai.Completion.create(
  engine="text-davinci-003",
  prompt="Translate the following English text to French: '{}'",
  max_tokens=60
)

print(response.choices[0].text.strip())

In this example, the prompt is a text string that you want the model to process. The max_tokens parameter specifies the maximum length of the output. You would replace 'your-api-key' with your actual OpenAI API key.

Please note that this is a general example and might need to be adjusted based on the specific requirements of your document processing task.

For more detailed information on how to use the OpenAI API, you can refer to the OpenAI API documentation (https://platform.openai.com/docs/).

If you want to process documents in a more complex way, such as extracting structured data or using custom functions, you might need to use OpenAI's fine-tuning capability. Fine-tuning allows you to train the model on your own data to perform specific tasks. For more information on fine-tuning, you can refer to the OpenAI fine-tuning guide (https://platform.openai.com/docs/guides/fine-tuning).

#

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

flint adder
#

@gritty kraken What if I have split the document and have the sesrch functionality implemented, how do I pass the results to the LLM ?

gritty krakenBOT
#

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

#

If you have split the document and implemented the search functionality, you can pass the results to the Language Learning Model (LLM) by including them in the conversation history. The LLM uses the conversation history to generate responses, so any information included in the history will be considered by the model.

Here's an example of how you might do this:

import openai

openai.api_key = 'your-api-key'

# Let's say you have a function that searches your documents and returns the results
def search_documents(query):
    # Implement your document search functionality here
    pass

# User's query
query = "What is the capital of France?"

# Search the documents for the answer
search_results = search_documents(query)

# Include the search results in the conversation history
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": query},
    {"role": "assistant", "content": search_results},
]

# Generate a response from the LLM
response = openai.ChatCompletion.create(
  model="gpt-4o-2024-08-06",
  messages=messages,
)

print(response.choices[0].message['content'])

In this example, the search_documents function is a placeholder for your document search functionality. You would need to replace it with the actual function that searches your documents and returns the results.

The search results are included in the messages list, which represents the conversation history. The LLM generates a response based on this history.

Please note that this is a general example and might need to be adjusted based on the specific requirements of your application.

#

For more detailed information on how to use the OpenAI API, you can refer to the OpenAI API documentation (https://platform.openai.com/docs/).

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

#

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

flint adder
#

@gritty kraken Please let me know why i’m getting “No Object” error at “response.tools_calls” here. The problem might be in the function definitions or implementation as the same code seems to work for another chatbot. Here are the relevant code parts:

gritty krakenBOT
#

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

#

I'm sorry, but I can't provide the help you're looking for. The code you're referring to wasn't included in your question, and it's not available in the provided knowledge sources. To help you debug the issue, I would need to see the relevant parts of your code, especially the function definitions and the part where you're trying to access response.tool_calls.

In general, a "No Object" error might occur if you're trying to access a property on an object that doesn't exist. In this case, it could mean that the response object doesn't have a tool_calls property. This could happen if the API call didn't return any tool calls, or if there was an error in the API call.

If you're using OpenAI's Chat API, tool calls are included in the message object in the choices array of the response. You can access them like this:

tool_calls = response['choices'][0]['message']['tool_calls']

Please make sure that your API call is successful and that it's returning tool calls. You can check the API response to see if it includes tool calls.

For more information on how to use tool calls with the Chat API, you can refer to the OpenAI API documentation (https://platform.openai.com/docs/).

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

#

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

flint adder
#

@gritty kraken def get_opensearch_results(source_fields: list, start_date: str, end_date: str) -> dict:

# Define the base query structure
match_all_query = {
    "size": 10000,
    "_source": source_fields,
    "query": {
        "bool": {
            "must": [],
            "filter": [
                {
                    "match_all": {}
                },
                {
                    "range": {
                        "collected_on": {
                            "gte": start_date,
                            "lte": end_date,
                            "format": "strict_date_optional_time"
                        }
                    }
                }
            ],
            "should": [],
            "must_not": []
        }
    }
}

max_attempts = 5
attempt = 0
while attempt < max_attempts:
    try:
        resp = Opensearch_client.search(
            index='prd_helix_telescope*',
            body=match_all,
            scroll='30m'
        )
        break  # Exit loop if successful
    except exceptions.ConnectionTimeout as e:
        attempt += 1
        wait_time = 2 ** attempt  # Exponential backoff
        return f"Attempt {attempt} failed with timeout: {str(e)}. Retrying in {wait_time} seconds..."
        sleep(wait_time)
else:
    raise Exception("Max retries reached. Unable to complete the request.")

# If successful, continue with the rest of the processing
old_scroll_id = resp['_scroll_id']
lst = []
while len(resp['hits']['hits']):
    lst.append(resp['hits']['hits'])
    # Make a request using the Scroll API
    try:
        resp = Opensearch_client.scroll(
            scroll_id=old_scroll_id,
            scroll='30m'
        )
    except exceptions.ConnectionTimeout as e:
gritty krakenBOT
#

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

#

The function get_opensearch_results you provided is designed to interact with an OpenSearch service (formerly known as Elasticsearch). It takes a list of source fields and a date range as input, and returns a dictionary of search results.

Here's a brief explanation of what the function does:

  1. It first defines a query structure that will be sent to the OpenSearch service. The query is designed to return up to 10,000 results that match all specified conditions and fall within the provided date range.

  2. It then attempts to send the query to the OpenSearch service. If the service doesn't respond due to a connection timeout, it will retry the request up to 5 times, with an exponential backoff delay between each attempt.

  3. If the request is successful, it enters a loop where it continues to retrieve results from the OpenSearch service using the Scroll API until no more results are available. The Scroll API is used to retrieve large numbers of results from OpenSearch in an efficient manner.

  4. All the results are appended to a list, which is returned at the end of the function.

Please note that this function requires a connection to an OpenSearch service, and it uses the Opensearch_client object to interact with the service. This object is not defined within the function, so it should be defined elsewhere in your code.

Also, the function seems to be incomplete as the last except block is not handling the exception. You might want to add error handling code there.

#

Unfortunately, the provided knowledge sources do not include information about OpenSearch or the specific libraries and methods used in this function. For more detailed information about how to use OpenSearch and the Scroll API, you can refer to the OpenSearch documentation (https://opensearch.org/docs/latest/).

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

#

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

flint adder
#

@gritty kraken rest of the code: s reached. Unable to complete the request.")

# If successful, continue with the rest of the processing
old_scroll_id = resp['_scroll_id']
lst = []
while len(resp['hits']['hits']):
    lst.append(resp['hits']['hits'])
    # Make a request using the Scroll API
    try:
        resp = Opensearch_client.scroll(
            scroll_id=old_scroll_id,
            scroll='30m'
        )
    except exceptions.ConnectionTimeout as e:
        return f"Scroll API failed with timeout: {str(e)}"
        break  # Exit the loop if scrolling fails

    old_scroll_id = resp['_scroll_id']

overall_lst = []

for batch in lst:
    for records in batch:
        overall_lst.append(records)

# Return the overall collected data to the LLM
result = {
    "status": "success",
    "total_records": len(overall_lst),
    "data": overall_lst
}

return json.dumps(result)
gritty krakenBOT
#

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

#

The provided code is the continuation of the get_opensearch_results function. After successfully making a request to the OpenSearch service, it enters a loop where it continues to retrieve results using the Scroll API until no more results are available.

Here's a brief explanation of the rest of the function:

  1. It initializes an empty list lst and enters a loop that continues as long as there are hits in the response.

  2. Inside the loop, it appends the hits to the lst list and makes a new Scroll API request using the previous scroll ID.

  3. If the Scroll API request fails due to a connection timeout, it returns an error message and breaks the loop.

  4. After exiting the loop, it initializes an empty list overall_lst and iterates over the batches in lst. For each batch, it iterates over the records and appends them to overall_lst.

  5. Finally, it constructs a result dictionary that includes the status, the total number of records, and the data itself. It returns this dictionary as a JSON string.

Here's the provided code:

old_scroll_id = resp['_scroll_id']
lst = []
while len(resp['hits']['hits']):
    lst.append(resp['hits']['hits'])
    # Make a request using the Scroll API
    try:
        resp = Opensearch_client.scroll(
            scroll_id=old_scroll_id,
            scroll='30m'
        )
    except exceptions.ConnectionTimeout as e:
        return f"Scroll API failed with timeout: {str(e)}"
        break  # Exit the loop if scrolling fails

    old_scroll_id = resp['_scroll_id']

overall_lst = []

for batch in lst:
    for records in batch:
        overall_lst.append(records)

# Return the overall collected data to the LLM
result = {
    "status": "success",
    "total_records": len(overall_lst),
    "data": overall_lst
}

return json.dumps(result)
#

Please note that this function requires a connection to an OpenSearch service, and it uses the Opensearch_client object to interact with the service. This object is not defined within the function, so it should be defined elsewhere in your code.

For more detailed information about how to use OpenSearch and the Scroll API, you can refer to the OpenSearch documentation (https://opensearch.org/docs/latest/).

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