123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace App\Services\DeepSeek;
- use App\Consts\ErrorConst;
- use App\Facade\Site;
- use App\Libs\Utils;
- use GuzzleHttp\Client;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Redis;
- use OSS\Core\OssException;
- use OSS\OssClient;
- class DeepSeekService
- {
- private $url;
- private $api_key;
- private $client;
- private $headers;
- public function __construct() {
- $this->url = 'https://api.deepseek.com/chat/completions';
- $this->api_key = env('DEEPSEEK_API_KEY');
- $this->client = new Client();
- $this->headers = [
- 'Authorization' => 'Bearer '.$this->api_key,
- 'Content-Type' => 'application/json; charset=UTF-8'
- ];
- }
- // 与推理模型对话
- public function chatWithReasoner($data) {
- ini_set('max_execution_time', 0);
- $content = getProp($data, 'content');
- $model = getProp($data, 'model', 'r1');
- $model = $model == 'r1' ? 'deepseek-reasoner' : 'deepseek-chat';
-
- $messages = [
- [
- 'role' => 'system',
- 'content' => '下面有一段小说文本,请帮我将文本中的每句话按从上到下的顺序拆分成角色不同的剧本文稿(不得更改上下文顺序和内容),文稿形式严格按照“角色名(男或女):台词{情感}”输出,需要注意以下几点要求:
- 1.角色名后不要加入任何其他词语,只能加不包括旁白的性别,在男或女中选
- 2.非对话部分请全部用旁白角色代替
- 3.情感必须在【开心、悲伤、生气、惊讶、恐惧、厌恶、激动、冷漠、中性】中选一个,不得使用其他词语'
- ],
- [
- 'role' => 'user',
- 'content' => $content
- ]
- ];
- $post_data = [
- 'model' => $model, // R1模型: deepseek-reasoner V3模型: deepseek-chat
- 'messages' => $messages,
- 'max_tokens' => 8192,
- 'temperature' => 1, // 采样温度,介于 0 和 2 之间。更高的值,如 0.8,会使输出更随机,而更低的值,如 0.2,会使其更加集中和确定。 我们通常建议可以更改这个值或者更改 top_p,但不建议同时对两者进行修改。
- // 'top_p' => 1, // 作为调节采样温度的替代方案(<=1),模型会考虑前 top_p 概率的 token 的结果。所以 0.1 就意味着只有包括在最高 10% 概率中的 token 会被考虑。 我们通常建议修改这个值或者更改 temperature,但不建议同时对两者进行修改。
- 'frequency_penalty' => 0, // 介于 -2.0 和 2.0 之间的数字。如果该值为正,那么新 token 会根据其在已有文本中的出现频率受到相应的惩罚,降低模型重复相同内容的可能性。
- 'presence_penalty' => 0, // 介于 -2.0 和 2.0 之间的数字。如果该值为正,那么新 token 会根据其是否已在已有文本中出现受到相应的惩罚,从而增加模型谈论新主题的可能性。
- 'response_format' => [
- 'type' => 'text' // 默认值text,回答的结果输出文字(非接口返回值是text,接口返回值还是json字串),还可选:json_object,输出json格式
- ],
- 'stream' => false // 是否流式输出,如果设置为 True,将会以 SSE(server-sent events)的形式以流式发送消息增量。消息流以 data: [DONE] 结尾。
- ];
- $result = $this->client->post($this->url, ['json' => $post_data, 'headers' => $this->headers]);
- $response = $result->getBody()->getContents();
- $response_arr = json_decode($response, true);
- $update_data = [];
- $content = '';
- if (isset($response_arr['choices']) && count($response_arr['choices']) > 0) {
- $content = isset($response_arr['choices'][0]['message']['content']) ? $response_arr['choices'][0]['message']['content'] : '';
- $update_data = [
- 'role' => 'assistant',
- 'content' => $response_arr['choices'][0]['message']['content'],
- 'usage' => isset($response_arr['usage']) ? $response_arr['usage'] : []
- ];
- }
- // 处理获取到的剧本数据
- $script_content = handleScriptWords($content);
- $result = [
- 'origin_content' => $content,
- 'roles' => getProp($script_content, 'roles'),
- 'words' => getProp($script_content, 'words'),
- ];
- return $result;
- }
- // 文字合成语音(火山引擎)
- public function tts($data) {
- $url = 'https://openspeech.bytedance.com/api/v1/tts';
- $headers = [
- 'Authorization' => 'Bearer;'.env('VOLC_TOKEN'),
- 'Content-Type' => 'application/json; charset=UTF-8'
- ];
- $post_data = [
- 'app' => [
- 'appid' => env('VOLC_APPID'),
- 'token' => env('VOLC_TOKEN'),
- 'cluster' => 'volcano_tts'
- ],
- 'user' => [
- 'uid' => 'mp_audio'
- ],
- // 'audio' => [
- // 'voice_type' =>
- // ],
- ];
- }
- }
|