用ModelArts Studio实现deepseek问答自由
【摘要】 1、华为云官网开展了一个免费体验deepseek满血版活动,按照活动指引,可以领取6份200万的token api 调用。2、然后点击“调用”按钮,获取api调用的主要参数:API URL、Model ID、及API Key。注意API地址用在IDE里时,需要去掉最后一个v1后面的内容。3、打开vs code,安装cline插件,在cline插件的设置里,选择 openai 兼容模型,这样就...
1、华为云官网开展了一个免费体验deepseek满血版活动,按照活动指引,可以领取6份200万的token api 调用。
2、然后点击“调用”按钮,获取api调用的主要参数:API URL、Model ID、及API Key。注意API地址用在IDE里时,需要去掉最后一个v1后面的内容。
3、打开vs code,安装cline插件,在cline插件的设置里,选择 openai 兼容模型,这样就可以自定义模型参数了,在这里把我们在ModelArts Studio里领取的deepseek v3大模型参数填进去。
4、最后在cline的对话框里就可以随意输入你想问的问题了,例如天气怎么样。当然由于cline是个编程插件,它会在你的问题上加一些编程prompt,所以回复的就非常程序员思维了。
5、我想自由问答,不限于编程怎么办呢,把ModelArts Studio的调用示例代码拷贝到vs code,示例代码固定了一个 “你好”的问题,打开cline的对话框,输入“把当前代码修改为可以多轮对话聊天” ,这样它会自动修改代码,咱们就可以多轮对话了
附cline修改后的完整代码:
# coding=utf-8
import requests
import json
import os
import urllib3
# Disable SSL verification
os.environ['CURL_CA_BUNDLE'] = ''
urllib3.disable_warnings()
# API configuration
API_URL = "https://infer-modelarts-cn-southwest-2.modelarts-infer.com/v1/infers/fd53915b-8935-48fe-be70-449d76c0fc87/v1/chat/completions"
API_KEY = "xxx"
def chat():
print("欢迎使用多轮对话聊天程序!输入'退出'来结束对话。")
# Initialize conversation history
messages = [
{"role": "system", "content": "You are a helpful assistant."}
]
while True:
user_input = input("你: ")
if user_input.lower() in ['退出', 'exit']:
print("聊天结束,再见!")
break
# Add user message to history
messages.append({"role": "user", "content": user_input})
try:
# Prepare request
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {API_KEY}'
}
data = {
"model": "DeepSeek-V3",
"max_tokens": 200,
"messages": messages,
"stream": False,
"temperature": 1.0
}
# Send request
resp = requests.post(API_URL, headers=headers, data=json.dumps(data), verify=False)
if resp.status_code == 200:
response_data = resp.json()
assistant_message = response_data['choices'][0]['message']['content']
print(f"AI: {assistant_message}")
# Add assistant response to history
messages.append({"role": "assistant", "content": assistant_message})
else:
print(f"API请求失败,状态码: {resp.status_code}")
print(f"错误信息: {resp.text}")
except Exception as e:
print(f"发生错误: {str(e)}")
if __name__ == "__main__":
chat()
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)