チャットボットUIとChatGLM2-6Bの統合

0.背景

Chatbot UIとChatGLM2-6Bの統合を試みます。ChatGLM2-6BはAPIサービスを提供し、Chatbot UIはChatGPTインターフェースとOpenAIチャットモデルを模倣した機能を提供します。

結果を示す、

ここに画像の説明を挿入

1. チャットボット UI を導入する

「チャットボット UI をローカルに展開する」の記事を参照してください

2.ChatGLM2-6Bの導入

「ChatGLM2-6B のローカル展開」の記事を参照してください

3. ChatGLM2-6Bプロジェクトのopenai_api.pyを変更する

openai_api.py を直接起動すると、次のエラーが報告されます。

PydanticDeprecatedSince20: The `json` method is deprecated; use `model_dump_json` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.0.2/migration/
  yield "{}".format(chunk.json(exclude_unset=True, ensure_ascii=False))

略

  File "/root/miniconda3/envs/chatglm2/lib/python3.10/site-packages/pydantic/main.py", line 926, in json
    raise TypeError('`dumps_kwargs` keyword arguments are no longer supported.')
TypeError: `dumps_kwargs` keyword arguments are no longer supported.

特定のエラーの説明については、 https://errors.pydantic.dev/2.0.2/migration/の手順を参照してください

解決策は次のとおりです。

vi openai_api.py

--- 修改内容,有3处
    #yield "{}".format(chunk.json(exclude_unset=True, ensure_ascii=False))
    yield "{}".format(chunk.model_dump_json(exclude_unset=True))
---

(オプション) より高い適応性を実現するには、/v1で始まらない API エンドポイントを追加します。

class ChatCompletionResponse(BaseModel):
    略
    usage: Optional[Dict[str, int]] = {'prompt_tokens': 1, 'completion_tokens': 1, 'total_tokens': 2}

@app.get("/models", response_model=ModelList)
async def list_models():
    global model_args
    model_card = ModelCard(id="gpt-3.5-turbo")
    return ModelList(data=[model_card])

@app.post("/chat/completions", response_model=ChatCompletionResponse)
async def create_v1_chat_completion(request: ChatCompletionRequest):
    return RedirectResponse("/v1/chat/completions", status_code=307)

変更が完了したら、openai_api.pyを起動し、

python openai_api.py

4. チャットボットUIの構成を変更する

.env.local の設定を変更します。

vi .env.local

---
OPENAI_API_HOST=http://localhost:8000
OPENAI_API_KEY=none
---

チャットボットUIを起動し、

npm run dev

5. チャットボット UI にアクセスする

ブラウザでhttp://localhost:3000を開き、

ここに画像の説明を挿入
聞いて、聞いて、清華大学はどこですか?

ここに画像の説明を挿入
終わり!

おすすめ

転載: blog.csdn.net/engchina/article/details/131631767
おすすめ