首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

(2025)Unity调用DeepSeek-V3 API (兼容OpenAI SDK)

  • 25-03-04 18:00
  • 3531
  • 10437
blog.csdn.net

DeepSeek模型简介

DeepSeek-V3 为自研 MoE 模型,671B 参数,激活 37B,在 14.8T token 上进行了预训练。该模型多项评测成绩超越了 Qwen2.5-72B 和 Llama-3.1-405B 等其他开源模型,并在性能上和世界顶尖的闭源模型 GPT-4o 以及 Claude-3.5-Sonnet 不分伯仲。 

且DeepSeek为国内模型,访问无需魔法上网,以下是如何在Unity调用DeepSeek-V3 API。

首先,在DeepSeek官方平台注册账号并登录,申请API Key

根据官方文档封装Post方法如下:

  1. ///
  2. /// post请求
  3. ///
  4. ///
  5. ///
  6. ///
  7. ///
  8. ///
  9. public static IEnumerator PostRequest<T>(string token, Action onOver, object data, string urlTail = null)
  10. {
  11. using (UnityWebRequest request = new UnityWebRequest("https://api.deepseek.com/chat/completions" + urlTail))
  12. {
  13. request.method = "post";
  14. // 设置请求体内容
  15. string jsonStr = JsonConvert.SerializeObject(data);
  16. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonStr);
  17. request.uploadHandler = new UploadHandlerRaw(bodyRaw);
  18. request.SetRequestHeader("Content-Type", "application/json");
  19. request.downloadHandler = new DownloadHandlerBuffer();
  20. if (token != "")
  21. {
  22. // 添加请求头部信息
  23. request.SetRequestHeader("Authorization", token);
  24. }
  25. // 发送请求并等待响应
  26. yield return request.SendWebRequest();
  27. // 处理响应
  28. if (request.result == UnityWebRequest.Result.Success)
  29. {
  30. Debug.Log("新post请求(" + request.url + ")成功,返回结果:" + request.downloadHandler.text);
  31. // 在这里处理API响应
  32. onOver.Invoke(JsonConvert.DeserializeObject(request.downloadHandler.text));
  33. }
  34. else
  35. {
  36. Debug.Log("新post请求(" + request.url + ")失败,返回结果:" + request.downloadHandler.text);
  37. // 在这里处理API请求失败的情况
  38. onOver.Invoke(default(T));
  39. }
  40. }
  41. }

调用方法

传入token为 “Bearer ”,DeepSeek API Key为上面获取到的Key,JSON类组成如下:

JSON属性解析

        1.指定 model='deepseek-chat' 即可调用 DeepSeek-V3。

        2.将 stream 设置为 true 来使用流式输出。messages是传入的文本。

JSON代码如下:

  1. public class DeepSeekJson
  2. {
  3. public string model { get; set; }
  4. public List messages { get; set; }
  5. public bool stream { get; set; }
  6. public class DeepSeekItem
  7. {
  8. public string role { get; set; }
  9. public string content { get; set; }
  10. }
  11. }

Python OUTPUT样例代码如下:

  1. import json
  2. from openai import OpenAI
  3. client = OpenAI(
  4. api_key="",
  5. base_url="https://api.deepseek.com",
  6. )
  7. system_prompt = """
  8. The user will provide some exam text. Please parse the "question" and "answer" and output them in JSON format.
  9. EXAMPLE INPUT:
  10. Which is the highest mountain in the world? Mount Everest.
  11. EXAMPLE JSON OUTPUT:
  12. {
  13. "question": "Which is the highest mountain in the world?",
  14. "answer": "Mount Everest"
  15. }
  16. """
  17. user_prompt = "Which is the longest river in the world? The Nile River."
  18. messages = [{"role": "system", "content": system_prompt},
  19. {"role": "user", "content": user_prompt}]
  20. response = client.chat.completions.create(
  21. model="deepseek-chat",
  22. messages=messages,
  23. response_format={
  24. 'type': 'json_object'
  25. }
  26. )
  27. print(json.loads(response.choices[0].message.content))

注意事项​

  1. 设置 response_format 参数为 {'type': 'json_object'}。

  2. 用户传入的 system 或 user prompt 中必须含有 json 字样,并给出希望模型输出的 JSON 格式的样例,以指导模型来输出合法 JSON。

  3. 需要合理设置 max_tokens 参数,防止 JSON 字符串被中途截断。

 Python使用上下文拼接样例代码:

  1. from openai import OpenAI
  2. client = OpenAI(api_key="", base_url="https://api.deepseek.com")
  3. # Round 1
  4. messages = [{"role": "user", "content": "What's the highest mountain in the world?"}]
  5. response = client.chat.completions.create(
  6. model="deepseek-chat",
  7. messages=messages
  8. )
  9. messages.append(response.choices[0].message)
  10. print(f"Messages Round 1: {messages}")
  11. # Round 2
  12. messages.append({"role": "user", "content": "What is the second?"})
  13. response = client.chat.completions.create(
  14. model="deepseek-chat",
  15. messages=messages
  16. )
  17. messages.append(response.choices[0].message)
  18. print(f"Messages Round 2: {messages}")

Unity调用以上样例代码方式和普通调用方式类似。

注:本文转载自blog.csdn.net的mYoCaRdiA的文章"https://blog.csdn.net/m0_70478643/article/details/144883296"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2491) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

134
游戏
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top