Skip to content

Building a Chatbot with Qwen/QwQ-32B

Create a basic chatbot with Qwen/QwQ-32B in a few steps.

Prerequisites

  • An API key from DeepRequest.io
  • Python 3.6 or later
  • Requests library (pip install requests)

Example Code

import requests
import json
api_key = "YOUR_API_KEY_HERE"
url = "https://api.deeprequest.io/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
def get_response(user_input, conversation_history=None):
if conversation_history is None:
conversation_history = []
# Add user message to history
conversation_history.append({"role": "user", "content": user_input})
data = {
"model": "qwq-32b",
"messages": conversation_history,
"max_tokens": 500,
"temperature": 0.7
}
response = requests.post(url, headers=headers, json=data)
response_data = response.json()
assistant_message = response_data["choices"][0]["message"]["content"]
# Add assistant response to history
conversation_history.append({"role": "assistant", "content": assistant_message})
return assistant_message, conversation_history
# Example usage
conversation = []
while True:
user_input = input("You: ")
if user_input.lower() in ["exit", "quit", "bye"]:
break
bot_response, conversation = get_response(user_input, conversation)
print(f"Bot: {bot_response}")

How It Works

  1. We send a request to the API with:

    • Model identifier: qwq-32b
    • Messages: An array of conversation messages with roles and content
    • Temperature: Controls randomness (0.7 is balanced)
    • Max tokens: Limits response length
  2. The API returns a response we can display to the user

  3. We maintain a conversation history to give the model context of previous exchanges

Next Steps

  • Add a system message to set the chatbot’s personality
  • Implement error handling for API calls
  • Create a web interface with Flask or Streamlit