国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

DeepSeek 推理模型接入Webman Ai 顯示思考過程

hsk99

支持顯示思考過程,并在上下文拼接時過濾掉思考信息減少輸入tokens

體驗地址

https://mjai.top/#role=55

效果圖

截圖

代碼

位置:plugin\ai\app\handler\DeepSeekReasoner.php

<?php

namespace plugin\ai\app\handler;

class DeepSeekReasoner extends Base
{
    /**
     * @var string 模型處理器名稱
     */
    protected static $name = 'DeepSeekReasoner';

    /**
     * @var string 模型類型
     */
    protected static $type = 'deepseek-reasoner';

    /**
     * @var string[] 支持的模型名稱
     */
    public static $models = [
        'deepseek-reasoner',
    ];

    /**
     * @var string[] 自定義配置
     */
    public static $defaultSettings = [
        'api' => [
            'name' => 'API',
            'type' => 'text',
            'value' => 'https://api.deepseek.com',
        ],
        'apikey' => [
            'name' => 'ApiKey',
            'type' => 'text',
            'value' => '',
        ],
        'regFreeCount' => [
            'name' => '注冊贈送',
            'type' => 'number',
            'value' => 0,
        ],
        'dayFreeCount' => [
            'name' => '每日贈送',
            'type' => 'number',
            'value' => 0,
        ],
    ];

    /**
     * @var string 處理器
     */
    protected $driverClass = driver\DeepSeekReasoner::class;

    /**
     * 對話
     * @param $data
     * @param $options
     * @return void
     */
    public function completions($data, $options)
    {
        $this->driver = new $this->driverClass($this->getSettings());
        $this->driver->completions($data, $options);
    }
}

位置:plugin\ai\app\handler\driver\DeepSeekReasoner.php

<?php

namespace plugin\ai\app\handler\driver;

use Throwable;

class DeepSeekReasoner extends Gpt
{
    /**
     * @var string api地址
     */
    protected $api = 'https://api.deepseek.com';

    /**
     * @var bool 是否正在推理
     */
    protected $reasoning = false;

    /**
     * @param array $data
     * @param array $options
     * @return void
     * @throws Throwable
     */
    public function completions(array $data, array $options)
    {
        if (!empty($data['messages'])) {
            $data['messages'] = array_map(function ($item) {
                $start = "```thinking...\n";
                $end = "\n```\n\n------------\n\n";

                if ($item['role'] === 'assistant' && strpos($item['content'], $start) === 0) {
                    $endPos = strpos($item['content'], $end);
                    if ($endPos !== false) {
                        $endPos += strlen($end);
                        $item['content'] = substr($item['content'], $endPos);
                    }
                }

                return $item;
            }, $data['messages']);
        }

        if (isset($options['stream'])) {
            $options['stream'] = function ($data) use ($options) {
                $data = array_merge(['content' => ''], $data);
                unset($data['model']);

                $delta = $data['choices'][0]['delta'] ?? [];
                $content = $delta['content'] ?? ($delta['reasoning_content'] ?? '');
                if (($delta['content'] ?? '') === '' && isset($delta['reasoning_content'])) {
                    $content = $delta['reasoning_content'];
                }

                if (isset($delta['reasoning_content']) && empty($delta['content'])) {
                    if (!$this->reasoning) {
                        $this->reasoning = true;
                        $content = "```thinking...\n\n" . $content;
                    }
                } else if ($this->reasoning) {
                    $this->reasoning = false;
                    $content = "\n```\n\n------------\n\n" . $content;
                }

                if ($content === "<think>") {
                    $content = "```thinking...\n\n";
                } else if ($content === "</think>") {
                    $content = "\n```\n\n------------\n\n";
                }

                $data['content'] = $content;
                $options['stream']($data);
            };
        }

        return parent::completions($data, $options);
    }

    /**
     * @param $buffer
     * @return array|array[]|mixed
     */
    public static function formatResponse($buffer)
    {
        if (!$buffer || $buffer[0] === '') {
            return [
                'error' => [
                    'code' => 'parse_error',
                    'message' => 'Empty response from api',
                    'detail' => $buffer
                ]
            ];
        }
        $json = json_decode($buffer, true);
        if ($json) {
            return $json;
        }
        $chunks = explode("\n", $buffer);
        $content = '';
        $finishReason = null;
        $model = '';
        $promptFilterResults = null;
        $contentFilterResults = null;
        $contentFilterOffsets = null;
        $toolCalls = [];
        $reasoning = false;
        foreach ($chunks as $chunk) {
            $chunk = trim($chunk);
            if ($chunk === "") {
                continue;
            }
            if (strpos($chunk, 'data:{') === 0) {
                $chunk = substr($chunk, 5);
            } else {
                $chunk = substr($chunk, 6);
            }
            if ($chunk === "" || $chunk === "[DONE]") {
                continue;
            }
            try {
                $data = json_decode($chunk, true);
                if (isset($data['model'])) {
                    $model = $data['model'];
                }
                if (isset($data['prompt_filter_results'])) {
                    $promptFilterResults = $data['prompt_filter_results'];
                }
                if (isset($data['error'])) {
                    $content .= $data['error']['message'] ?? "";
                } else {
                    foreach ($data['choices'] ?? [] as $item) {
                        if (isset($item['delta']['reasoning_content']) && empty($item['delta']['content'])) {
                            if (!$reasoning) {
                                $reasoning = true;
                                $content .= "```thinking...\n\n";
                            }
                            $content .= $item['delta']['reasoning_content'] ?? '';
                        } else {
                            if ($reasoning) {
                                $reasoning = false;
                                $content .= "\n```\n\n------------\n\n";
                            }

                            if (!empty($item['delta']['content']) && $item['delta']['content'] === "<think>") {
                                $content .= "```thinking...\n\n";
                            } else if (!empty($item['delta']['content']) && $item['delta']['content'] === "</think>") {
                                $content .= "\n```\n\n------------\n\n";
                            } else {
                                $content .= $item['delta']['content'] ?? '';
                            }
                        }

                        foreach ($item['delta']['tool_calls'] ?? [] as $function) {
                            if (isset($function['function']['name'])) {
                                $toolCalls[$function['index']] = $function;
                            } elseif (isset($function['function']['arguments'])) {
                                $toolCalls[$function['index']]['function']['arguments'] .= $function['function']['arguments'];
                            }
                        }
                        if (isset($item['finish_reason'])) {
                            $finishReason = $item['finish_reason'];
                        }
                        if (isset($item['content_filter_results'])) {
                            $contentFilterResults = $item['content_filter_results'];
                        }
                        if (isset($item['content_filter_offsets'])) {
                            $contentFilterOffsets = $item['content_filter_offsets'];
                        }
                    }
                }
            } catch (Throwable $e) {
                echo $e;
            }
        }
        $result = [
            'choices' => [
                [
                    'finish_reason' => $finishReason,
                    'index' => 0,
                    'message' => [
                        'role' => 'assistant',
                        'content' => $content,
                    ],
                ]
            ],
            'model' => $model,
        ];
        if ($promptFilterResults) {
            $result['prompt_filter_results'] = $promptFilterResults;
        }
        if ($contentFilterResults) {
            $result['choices'][0]['content_filter_results'] = $contentFilterResults;
        }
        if ($contentFilterOffsets) {
            $result['choices'][0]['content_filter_offsets'] = $contentFilterOffsets;
        }
        if ($toolCalls) {
            $result['choices'][0]['message']['tool_calls'] = array_values($toolCalls);
        }
        return $result;
    }
}
1323 4 1
4個評論

ascuge

有點小bug,我在測試的時候碰到thinking跟回答混了。
截圖

  • hsk99 2025-02-11

    發(fā)的什么內(nèi)容?對接的是官方api還是第三方的

  • ascuge 2025-02-11

    官方的,就發(fā)了個9.11跟9.8誰大,然后發(fā)現(xiàn)思考過程截斷顯示成回答,然后又在思考內(nèi)容的樣式顯示結(jié)果。

wocall

  • 暫無評論
hsk99

我這邊沒出現(xiàn)這個情況

官方的api

截圖
截圖
截圖


硅基流動的api

截圖
截圖

  • ascuge 2025-02-11

    我這個思考過程很長,我還沒細看是不是輸出了什么字符導(dǎo)致截斷的?;仡^細看一下。您這個硅基是直接把api接口改成硅基的接口地址就可以了?

  • hsk99 2025-02-11

    對 api、模型換了就行

  • ascuge 2025-02-11

    好嘞,謝謝

owenzhang

https://github.com/hsk99/transfer-statistics
哥,你這個怎么都不更新了,年前還想用來著

  • 暫無評論

hsk99

225
積分
0
獲贊數(shù)
0
粉絲數(shù)
2019-06-29 加入
??