Custom GPT Models with Functions and Thread
- Taru Peters-Naug
- Jul 3, 2024
- 3 min read
Personalising your GPT implementation for your users and with your business logic seems like a heavy task. However with Open AI service providing threads and function capabilities we now have access to do just that within some simple code implementations.
In this article we won't dive into the technicalities of implementation, if you want access to a github repository with examples get in touch.
At its core LLMs are token / language models, they are built around the ability to predict words and tokens based on context and history. When using off the shelf models they have been trained on large datasets to give them the knowledge they have, which is very useful, but in business we need to take this a step further. Have our models perform tasks unique to our business and to our customers
Threads
Lets start with the customers, before going any further we need to ensure that our models can retain a memory of their conversation with a customer so they can retain their context and provide personalised service. This is done using threads. A thread ID needs to be created that correlates to your customers unique identifier. A simple implementation would look something like this.
def initialize_thread():
# Create a new thread
thread = client.beta.threads.create()
return thread.id
def predict():
text = request.get_json().get("message")
if 'thread_id' not in session:
session['thread_id'] = initialize_thread() # Initialize thread and store in session
thread_id = session['thread_id'] # Retrieve the thread ID from the session
# Get the response using the specific thread_id
response = get_openai_response(text, thread_id)
return jsonify({"answer": response})
Now your customers can have long running conversations rather than a simple question answer. This becomes especially useful when you can integrate with business functionality to give customers access to information unique to them that only your business can provide.
Functions
Functions are a long standing concept in programming, simple pieces of logic that are housed in a space that they can be called with given inputs and outputs. For example a function for returning the weather may take an input location and return the weather.
def get_weather(city: str) -> str:
base_url = "http://api.openweathermap.org/data/2.5/weather"
params = {
'q': city,
'appid': openweatherapikey,
'units': 'metric' # Use 'imperial' for Fahrenheit
}
response = requests.get(base_url, params=params)
if response.status_code == 200:
data = response.json()
weather_description = data['weather'][0]['description']
temperature = data['main']['temp']
return f"The current weather in {city} is {weather_description} with a temperature of {temperature}°C."
else:
return f"Could not retrieve weather data for {city}. Please check the city name or try again later."
Now these are nothing new, however the gamechanger with generative AI is that you can use plain english to call these functions. Once they are defined and your model is given access to them a user can ask in many different ways for information and the model will know to use a function that is provided to it. This is done with a structured definition as shown below.
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a given city",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The name of the city to get the weather for"
},
},
"required": ["city"]
}
}
}
]
What Does This Mean?
While the examples provided may seem mundane and technical, consider how these simple features can revolutionize your business and processes. Personalized automated conversations with customers that integrate internal business logic can significantly enhance customer experience.
If your business has existing code or custom software, you likely have functions and APIs available. These can be provided to your model with plain English definitions and integrated into your business's custom LLMs, leading to a powerful, personalized AI implementation.
Kommentare