The Qwen API, by default, does not store your conversation history. The multi-round conversation feature equips the model with the ability to "remember" past interactions, catering to scenarios such as follow-up questions and information gathering.
Performance showcase
This is for reference only and no requests are actually sent.
How to use
If you need the model to refer to conversation history, you need to input the history along with new instructions into the Qwen API.
Prerequisites
You must first obtain an API key and set the API key as an environment variable. If you need to use OpenAI SDK or DashScope SDK, you must install the SDK.
Get started
To facilitate multi-round conversations, you must maintain a messages array. For each round of conversation, add the history and new instructions to the messages array in the {"role": "xxx", "content": "xxx"}
format.
OpenAI compatible
Python
The sample code builds a phone shopping guide that engages in multi-round conversations with a customer to gather purchase intentions. After all information is collected, the conversation is concluded.
import os
from openai import OpenAI
def get_response(messages):
client = OpenAI(
# If the environment variable is not configured, replace the following line with: api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
# Model list: https://www.alibabacloud.com/help/en/model-studio/getting-started/models
completion = client.chat.completions.create(model="qwen-plus", messages=messages)
return completion
# Initialize a messages array
messages = [
{
"role": "system",
"content": """You are a clerk at the Bailian phone store, responsible for recommending phones to users. Phones have two parameters: screen size (including 6.1 inches, 6.5 inches, 6.7 inches) and resolution (including 2K, 4K).
You can only ask the user one parameter at a time. If the user's information is incomplete, you need to ask a follow-up question to provide the missing parameter. If the parameter collection is complete, you should say: I have understood your purchase intention, please wait a moment.""",
}
]
assistant_output = "Welcome to the Bailian phone store. What is the size of the phone do you need to buy?"
print(f"Model output: {assistant_output}\n")
while "I have understood your purchase intention" not in assistant_output:
user_input = input("Please enter:")
# Add user question information to the messages list
messages.append({"role": "user", "content": user_input})
assistant_output = get_response(messages).choices[0].message.content
# Add the large model's response information to the messages list
messages.append({"role": "assistant", "content": assistant_output})
print(f"Model output: {assistant_output}")
print("\n")
Node.js
The sample code builds a phone shopping guide that engages in multi-round conversations with a customer to gather purchase intentions. After all information is collected, the conversation is concluded.
import OpenAI from "openai";
import { createInterface } from 'readline/promises';
// Define constants
const BASE_URL = "https://dashscope-intl.aliyuncs.com/compatible-mode/v1";
const openai = new OpenAI({
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: BASE_URL
});
async function getResponse(messages) {
try {
const completion = await openai.chat.completions.create({
// Model list: https://www.alibabacloud.com/help/en/model-studio/getting-started/models
model: "qwen-plus",
messages: messages,
});
return completion.choices[0].message.content;
} catch (error) {
console.error("Error fetching response:", error);
throw error; // Rethrow the exception for upper-level handling
}
}
// Initialize messages array
const messages = [
{
"role": "system",
"content": `You are a clerk at the Bailian phone store, responsible for recommending phones to users. Phones have two parameters: screen size (including 6.1 inches, 6.5 inches, 6.7 inches) and resolution (including 2K, 4K).
You can only ask the user one parameter at a time. If the user's information is incomplete, you need to ask a follow-up question to provide the missing parameter. If the parameter collection is complete, you should say: I have understood your purchase intention, please wait a moment.`,
}
];
let assistant_output = "Welcome to the Bailian phone store. What is the size of the phone do you need to buy?";
console.log(assistant_output);
const readline = createInterface({
input: process.stdin,
output: process.stdout
});
(async () => {
while (!assistant_output.includes("I have understood your purchase intention")) {
const user_input = await readline.question("Please enter:");
messages.push({ role: "user", content: user_input});
try {
const response = await getResponse(messages);
assistant_output = response;
messages.push({ role: "assistant", content: assistant_output });
console.log(assistant_output);
console.log("\n");
} catch (error) {
console.error("Error fetching response:", error);
}
}
readline.close();
})();
cURL
curl -X POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-plus",
"messages":[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Hello"
},
{
"role": "assistant",
"content": "Hello, I am Qwen."
},
{
"role": "user",
"content": "What skills do you have?"
}
]
}'
DashScope
Python
The sample code illustrates a phone store guide engaging in multi-round conversations with a customer to gather purchase intentions, concluding once the collection is complete.
import os
from dashscope import Generation
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
def get_response(messages):
response = Generation.call(
# If the environment variable is not configured, replace the following line with: api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
# Model list: https://www.alibabacloud.com/help/en/model-studio/getting-started/models
model="qwen-plus",
messages=messages,
result_format="message",
)
return response
messages = [
{
"role": "system",
"content": """You are a clerk at the Bailian phone store, responsible for recommending phones to users. Phones have two parameters: screen size (including 6.1 inches, 6.5 inches, 6.7 inches) and resolution (including 2K, 4K).
You can only ask the user one parameter at a time. If the user's information is incomplete, you need to ask a follow-up question to provide the missing parameter. If the parameter collection is complete, you should say: I have understood your purchase intention, please wait a moment.""",
}
]
assistant_output = "Welcome to the Bailian phone store. What is the size of the phone do you need to buy?"
print(f"Model output: {assistant_output}\n")
while "I have understood your purchase intention" not in assistant_output:
user_input = input("Please enter:")
# Add user question information to the messages list
messages.append({"role": "user", "content": user_input})
assistant_output = get_response(messages).output.choices[0].message.content
# Add the large model's response information to the messages list
messages.append({"role": "assistant", "content": assistant_output})
print(f"Model output: {assistant_output}")
print("\n")
Java
import java.util.ArrayList;
import java.util.List;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import java.util.Scanner;
import com.alibaba.dashscope.protocol.Protocol;
public class Main {
public static GenerationParam createGenerationParam(List<Message> messages) {
return GenerationParam.builder()
// If the environment variable is not configured, replace the following line with: .apiKey("sk-xxx")
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// Model list: https://www.alibabacloud.com/help/en/model-studio/getting-started/models
.model("qwen-plus")
.messages(messages)
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
.build();
}
public static GenerationResult callGenerationWithMessages(GenerationParam param) throws ApiException, NoApiKeyException, InputRequiredException {
Generation gen = new Generation(Protocol.HTTP.getValue(), "https://dashscope-intl.aliyuncs.com/api/v1");
return gen.call(param);
}
public static void main(String[] args) {
try {
List<Message> messages = new ArrayList<>();
messages.add(createMessage(Role.SYSTEM, "You are a helpful assistant."));
for (int i = 0; i < 3;i++) {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter:");
String userInput = scanner.nextLine();
if ("exit".equalsIgnoreCase(userInput)) {
break;
}
messages.add(createMessage(Role.USER, userInput));
GenerationParam param = createGenerationParam(messages);
GenerationResult result = callGenerationWithMessages(param);
System.out.println("Model output: "+result.getOutput().getChoices().get(0).getMessage().getContent());
messages.add(result.getOutput().getChoices().get(0).getMessage());
}
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
e.printStackTrace();
}
System.exit(0);
}
private static Message createMessage(Role role, String content) {
return Message.builder().role(role.getValue()).content(content).build();
}
}
cURL
curl -X POST https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-plus",
"input":{
"messages":[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Hello"
},
{
"role": "assistant",
"content": "Hello, I am Qwen."
},
{
"role": "user",
"content": "What skills do you have?"
}
]
}
}'
Error codes
If the call failed and an error message is returned, see Error messages.
FAQ
Q: How is multi-round conversation billed?
A: Multi-round conversation is billed based on the number of input and output tokens. Note that the conversation history is included in the input tokens. Context cache can help reduce costs of multi-round conversations to some extent.