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

参考玩法

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

环境准备

包含了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"
  }
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
'http://0.0.0.0:4000/model/new' \
  • 1.

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)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 启动&效果
python main.py
  • 1.

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

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

说明

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