Skip to content

Commit 573dc8b

Browse files
committed
兼容 Ollama 等更多第三方的类 OpenAI 接口
1 parent 46f4a72 commit 573dc8b

File tree

3 files changed

+16
-23
lines changed

3 files changed

+16
-23
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ imi-ai 是一个 ChatGPT 开源项目,支持聊天、问答、写代码、写
9393
* [x] [ChatGLM3](https://github.com/THUDM/ChatGLM3)
9494
* [x] [Google Gemini](https://aistudio.google.com/)
9595
* [x] [Qwen2](https://github.com/QwenLM/Qwen2) (Gitee AI Serverless API)
96+
* [x] [Ollama](https://ollama.com/)
97+
98+
> 使用 Ollama 提供的类 OpenAI API,可以支持几乎所有开源模型私有化部署
9699
97100
### 其它
98101

server/Module/Chat/Service/ChatService.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -197,24 +197,21 @@ public function chatStream(string $id, int $memberId, string $ip): \Iterator
197197
{
198198
$role = $delta['role'];
199199
}
200-
else
200+
$yieldData = [];
201+
if ($finishReason = ($data['finish_reason'] ?? null))
201202
{
202-
$yieldData = [];
203-
if ($finishReason = ($data['finish_reason'] ?? null))
204-
{
205-
$yieldData['finishReason'] = $finishReason;
206-
}
207-
if (isset($delta['content']))
208-
{
209-
$content .= $delta['content'];
210-
$yieldData['content'] = $delta['content'];
211-
}
212-
if (!$yieldData)
213-
{
214-
continue;
215-
}
216-
yield $yieldData;
203+
$yieldData['finishReason'] = $finishReason;
204+
}
205+
if (isset($delta['content']))
206+
{
207+
$content .= $delta['content'];
208+
$yieldData['content'] = $delta['content'];
209+
}
210+
if (!$yieldData)
211+
{
212+
continue;
217213
}
214+
yield $yieldData;
218215
}
219216
$endTime = time();
220217
$inputTokens += (\count($messages) * $modelConfig->additionalTokensPerMessage) + $modelConfig->additionalTokensAfterMessages;

server/Module/OpenAI/Client/OpenAI/Client.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,9 @@ public function chat(array $params, ?int &$inputTokens = null, ?int &$outputToke
6060

6161
try
6262
{
63-
$contents = '';
6463
foreach ($this->client->chat()->createStreamed($params) as $response)
6564
{
6665
$data = $response->toArray();
67-
$content = $data['choices'][0]['delta']['content'] ?? null;
68-
if (null !== $content && '' !== $content)
69-
{
70-
$contents .= $content;
71-
}
72-
var_dump($data);
7366
yield $data;
7467
}
7568
$inputTokens = $data['usage']['prompt_tokens'] ?? 0;

0 commit comments

Comments
 (0)