百川
说明
支持的领域 / 任务:aigc
Baichuan2-Turbo系列模型是百川智能推出的大语言模型,采用搜索增强技术实现大模型与领域知识、全网知识的全面链接。
快速开始
前提条件
示例代码
以下示例展示了调用百川API对一个用户指令进行响应的代码。
说明
需要使用您的API-KEY替换示例中的 your-dashscope-api-key ,代码才能正常运行。
设置API-KEY
export DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEY
from http import HTTPStatus
import dashscope
def call_with_messages():
messages = [{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': '你好'}]
response = dashscope.Generation.call(
model='baichuan2-turbo',
messages=messages,
)
if response.status_code == HTTPStatus.OK:
print(response)
else:
print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
response.request_id, response.status_code,
response.code, response.message
))
if __name__ == '__main__':
call_with_messages()
// Copyright (c) Alibaba, Inc. and its affiliates.
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.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
public class Main{
public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
Generation gen = new Generation();
Message systemMsg = Message.builder()
.role(Role.SYSTEM.getValue())
.content("You are a helpful assistant.")
.build();
Message userMsg = Message.builder()
.role(Role.USER.getValue())
.content("如何做西红柿炒鸡蛋?")
.build();
GenerationParam param = GenerationParam.builder()
.model("baichuan2-turbo")
.messages(Arrays.asList(systemMsg, userMsg))
.build();
return gen.call(param);
}
public static void main(String[] args) {
try {
GenerationResult result = callWithMessage();
System.out.println(result);
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
// 使用日志框架记录异常信息
// Logger.error("An error occurred while calling the generation service", e);
System.err.println("An error occurred while calling the generation service: " + e.getMessage());
}
System.exit(0);
}
}
调用成功后,将会返回如下示例结果。
{
"output": {
"choices": [
{
"message": {
"content": "你好,有什么我可以帮助你的吗?",
"role": "assistant"
},
"index": 0,
"finish_reason": "stop"
}
]
},
"usage": {
"total_tokens": 12,
"input_tokens": 3,
"output_tokens": 9
},
"request_id": "8673e696-ee22-9ed0-9c35-b8673d4b87bc"
}
了解更多
有关百川大语言模型API的详细调用文档可前往API详情页面进行了解。
文档内容是否对您有帮助?