The GPT (Generative Pre-trained Transformer) API, such as OpenAI's GPT-3, maintains context and coherence over multiple API calls by relying on a few key mechanisms:
Context Window: GPT models have a maximum context window or token limit (for GPT-3, it's 4096 tokens). When making an API call, you can provide a prompt that includes some context from previous interactions. The model will generate a response based on this prompt, maintaining coherence with the provided context.
Session Management: To maintain context over multiple interactions, you need to manage the session on the client-side. This involves keeping track of the conversation history and including relevant parts of it in subsequent API calls. The context window limits how much history you can include, so you may need to truncate older parts of the conversation as new messages are added.
Statefulness: Some API implementations allow for a stateful session where the API maintains the context of the conversation across multiple calls. This feature abstracts away the need for the client to manage the context window, but it's not a standard feature of all GPT APIs.
Here is a simple example of how you might manage context across multiple API calls in Python using OpenAI's GPT-3 API:
import openai
# Initialize the API with your secret key
openai.api_key = 'your-api-key'
# Initial prompt to start the conversation
prompt = "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI:"
# Function to ask a question and get a response
def ask_gpt3(question, chat_log=None):
if chat_log is None:
chat_log = prompt # Start with the initial prompt if no history
else:
# Append the new question to the chat log, respecting the token limit
chat_log = truncate_chat_log(chat_log + f"\nHuman: {question}\nAI:", max_tokens=4096)
response = openai.Completion.create(
engine="davinci",
prompt=chat_log,
max_tokens=150,
temperature=0.7,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["\n", " Human:", " AI:"]
)
# The response includes the AI's reply, which we need to extract
answer = response.choices[0].text.strip()
# Update the chat log with the new response
return chat_log + answer, answer
# Function to maintain the chat log within the token limit
def truncate_chat_log(chat_log, max_tokens):
# Tokenize the chat log and truncate if necessary
tokens = openai.Tokenizer.encode(chat_log)
if len(tokens) > max_tokens:
start_index = len(tokens) - max_tokens
truncated_tokens = tokens[start_index:]
return openai.Tokenizer.decode(truncated_tokens)
return chat_log
# Example usage
chat_history = None
questions = ["What's the weather like in New York today?", "Can you give me a recipe for pancakes?"]
for question in questions:
chat_history, answer = ask_gpt3(question, chat_history)
print(f"AI: {answer}")
This example manages the session manually, ensuring that the context is maintained as long as it fits within the GPT API's token limit. If you reach the token limit, the truncate_chat_log
function removes the oldest part of the conversation to make room for new interactions.
In JavaScript, you can interact with the GPT API by making HTTP requests using libraries like axios
or fetch
. However, you would still need to manage the conversation context manually, as shown in the Python example.