Create your own chatbot
This tutorial will guide you through the process of creating your own simple chatbot using the Chat API (opens in a new tab). By following the provided steps, you'll be able to create an interactive LLM chatbot. For more information, visit this page.
Step 1: Get an API key
Prepare your API key to call the Chat API. If you don't have a key, you need to generate one by following the directions in the quick start guide.
Step 2: Create an OpenAI client
Since the Chat API is compatible with OpenAI chat completion requests, you can use the OpenAI library to make requests to Solar.
from openai import OpenAI
client = OpenAI(
api_key=UPSTAGE_API_KEY,
base_url="https://api.upstage.ai/v1/solar"
)
Step 3: Write a system prompt
Write a system prompt to use throughout the chat session.
# this system prompt will be used globally on the chat session.
system_prompt = {
"role": "system",
"content": "You are a helpful assistant."
}
Step 4: Write an simple interactive chatbot
This is an example of interacting with Solar. Use the client to build your own chatbot.
model = "solar-pro"
# Keep chat history in a list.
chat_history = []
# Set a limit for the chat history to manage the token count.
history_size = 10
while True:
# Step 1: Get user input.
user_prompt = input("User: ")
chat_history.append({
"role": "user",
"content": user_prompt
})
# Step 2: Use the chat history to generate the chatbot's response.
messages = [system_prompt] + chat_history
stream = client.chat.completions.create(
model=model,
messages=messages,
stream=True,
)
# Step 3: Output the Solar response chunks.
print("SOLAR: ", end="")
solar_response = ""
for chunk in stream:
response_content = chunk.choices[0].delta.content
if response_content is not None:
solar_response += response_content
print(response_content, end="")
print() # Finish response output.
# Step 4: Append the Solar response to the chat history.
chat_history.append({
"role": "assistant",
"content": solar_response
})
# Step 5: Ensure the chat history doesn't exceed the size limit.
chat_history = chat_history[:history_size]
Step 5: Let's chat!
Run your chatbot and enjoy chatting with Solar!
User: Hi, SOLAR!
SOLAR: Hello! How can I help you today?
User: What did I say?
SOLAR: You said "Hi, SOLAR!"