ollama+ fastapi + litellm proxy + chainlit 进行chatapp 快速开发

以前简单介绍过chainlit 以及litellm 以下是fastapi 集成chainlit ,使用litellm proxy 包装的标准openai 能力(同时还能实现计费)

参考玩法

环境准备

包含了litellm proxy 以及ollama 的比较简单,我就不多介绍了,具体可以通过静态配置或者api 动态配置

  • ollama 安装
    比较简单,选择对操作系统就可以了,注意需要先pull 使用的模型
  • litellm proxy 启动
    可以静态也可以动态
  • litellm 模型 api 配置模式(包含计费)
    注意是示例实际的结合自己的部署
curl -X 'POST' \
  'http://0.0.0.0:4000/model/new' \
  -H 'accept: application/json' \
  -H 'API-Key: sk-1234' \
  -H 'Content-Type: application/json' \
  -d '{
  "model_name": "dalongdemov3",
  "litellm_params": {
    "api_key": "demo",
    "api_base": "http://localhost:11434",
    "input_cost_per_token": 1,
    "output_cost_per_token": 1,
    "input_cost_per_second": 1,
    "output_cost_per_second": 1,
    "model": "ollama/qwen2:7b"
  }
}'

fastapi 集成chainlit

对于聊天使用的llm api 基于了litellm proxy 的openai 服务

  • 安装依赖
pip install fastapi chainlit openai 
  • chainlit app
    my_cl_app.py
import chainlit as cl
from openai import AsyncOpenAI
# 使用litellm 的openai 配置,key 可以通过界面生成
client = AsyncOpenAI(
    api_key="sk-ZTp5zuetNQoJNgG4xHgGzw",
    base_url="http://localhost:4000"
)
 
settings = {
    "model": "dalongdemov3",
    "temperature": 0,
}
 
@cl.on_message
async def on_message(message: cl.Message):
    response = await client.chat.completions.create(
        messages=[
            {
                "content": "You are a helpful bot, you always reply in chinese.",
                "role": "system"
            },
            {
                "content": message.content,
                "role": "user"
            }
        ],
        **settings
    )
    await cl.Message(content=response.choices[0].message.content).send()
 
@cl.on_chat_start
async def main():
    await cl.Message(content="你好").send()
  • fastapi 服务
    main.py
from fastapi import FastAPI
# mount_chainlit 提供了方便的集成能力
from chainlit.utils import mount_chainlit
 
app = FastAPI()
 
@app.get("/app")
def read_main():
    return {"message": "Hello World from main app"}
 
mount_chainlit(app=app, target="my_cl_app.py", path="/chainlit")
 
if __name__ == "__main__":
    import uvicorn  
    uvicorn.run(app, host="0.0.0.0", port=8000)
  • 启动&效果
python main.py

访问: http://localhost:8000/chainlit

说明

chainlit 提供了开箱即用的功能,对于快速开发一个chat app 还是很方便的,值得尝试下

参考资料

https://docs.chainlit.io/integrations/openai
https://github.com/Chainlit/chainlit
https://fastapi.tiangolo.com/
https://docs.litellm.ai/docs/
https://github.com/rongfengliang/ollama-litellm-fastapi-chainlit-chatapp

posted on 2024-08-11 01:04  荣锋亮  阅读(18)  评论(0编辑  收藏  举报

导航