yangdx
commited on
Commit
·
20ad069
1
Parent(s):
97ecf29
Remove lightrag_api_open_webui_demo.py file
Browse files
examples/lightrag_api_open_webui_demo.py
DELETED
@@ -1,140 +0,0 @@
|
|
1 |
-
from datetime import datetime, timezone
|
2 |
-
from fastapi import FastAPI
|
3 |
-
from fastapi.responses import StreamingResponse
|
4 |
-
import inspect
|
5 |
-
import json
|
6 |
-
from pydantic import BaseModel
|
7 |
-
from typing import Optional
|
8 |
-
|
9 |
-
import os
|
10 |
-
import logging
|
11 |
-
from lightrag import LightRAG, QueryParam
|
12 |
-
from lightrag.llm.ollama import ollama_model_complete, ollama_embed
|
13 |
-
from lightrag.utils import EmbeddingFunc
|
14 |
-
|
15 |
-
import nest_asyncio
|
16 |
-
|
17 |
-
WORKING_DIR = "./dickens"
|
18 |
-
|
19 |
-
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)
|
20 |
-
|
21 |
-
if not os.path.exists(WORKING_DIR):
|
22 |
-
os.mkdir(WORKING_DIR)
|
23 |
-
|
24 |
-
rag = LightRAG(
|
25 |
-
working_dir=WORKING_DIR,
|
26 |
-
llm_model_func=ollama_model_complete,
|
27 |
-
llm_model_name="qwen2.5:latest",
|
28 |
-
llm_model_max_async=4,
|
29 |
-
llm_model_max_token_size=32768,
|
30 |
-
llm_model_kwargs={"host": "http://localhost:11434", "options": {"num_ctx": 32768}},
|
31 |
-
embedding_func=EmbeddingFunc(
|
32 |
-
embedding_dim=1024,
|
33 |
-
max_token_size=8192,
|
34 |
-
func=lambda texts: ollama_embed(
|
35 |
-
texts=texts, embed_model="bge-m3:latest", host="http://127.0.0.1:11434"
|
36 |
-
),
|
37 |
-
),
|
38 |
-
)
|
39 |
-
|
40 |
-
with open("./book.txt", "r", encoding="utf-8") as f:
|
41 |
-
rag.insert(f.read())
|
42 |
-
|
43 |
-
# Apply nest_asyncio to solve event loop issues
|
44 |
-
nest_asyncio.apply()
|
45 |
-
|
46 |
-
app = FastAPI(title="LightRAG", description="LightRAG API open-webui")
|
47 |
-
|
48 |
-
|
49 |
-
# Data models
|
50 |
-
MODEL_NAME = "LightRAG:latest"
|
51 |
-
|
52 |
-
|
53 |
-
class Message(BaseModel):
|
54 |
-
role: Optional[str] = None
|
55 |
-
content: str
|
56 |
-
|
57 |
-
|
58 |
-
class OpenWebUIRequest(BaseModel):
|
59 |
-
stream: Optional[bool] = None
|
60 |
-
model: Optional[str] = None
|
61 |
-
messages: list[Message]
|
62 |
-
|
63 |
-
|
64 |
-
# API routes
|
65 |
-
|
66 |
-
|
67 |
-
@app.get("/")
|
68 |
-
async def index():
|
69 |
-
return "Set Ollama link to http://ip:port/ollama in Open-WebUI Settings"
|
70 |
-
|
71 |
-
|
72 |
-
@app.get("/ollama/api/version")
|
73 |
-
async def ollama_version():
|
74 |
-
return {"version": "0.4.7"}
|
75 |
-
|
76 |
-
|
77 |
-
@app.get("/ollama/api/tags")
|
78 |
-
async def ollama_tags():
|
79 |
-
return {
|
80 |
-
"models": [
|
81 |
-
{
|
82 |
-
"name": MODEL_NAME,
|
83 |
-
"model": MODEL_NAME,
|
84 |
-
"modified_at": "2024-11-12T20:22:37.561463923+08:00",
|
85 |
-
"size": 4683087332,
|
86 |
-
"digest": "845dbda0ea48ed749caafd9e6037047aa19acfcfd82e704d7ca97d631a0b697e",
|
87 |
-
"details": {
|
88 |
-
"parent_model": "",
|
89 |
-
"format": "gguf",
|
90 |
-
"family": "qwen2",
|
91 |
-
"families": ["qwen2"],
|
92 |
-
"parameter_size": "7.6B",
|
93 |
-
"quantization_level": "Q4_K_M",
|
94 |
-
},
|
95 |
-
}
|
96 |
-
]
|
97 |
-
}
|
98 |
-
|
99 |
-
|
100 |
-
@app.post("/ollama/api/chat")
|
101 |
-
async def ollama_chat(request: OpenWebUIRequest):
|
102 |
-
resp = rag.query(
|
103 |
-
request.messages[-1].content, param=QueryParam(mode="hybrid", stream=True)
|
104 |
-
)
|
105 |
-
if inspect.isasyncgen(resp):
|
106 |
-
|
107 |
-
async def ollama_resp(chunks):
|
108 |
-
async for chunk in chunks:
|
109 |
-
yield (
|
110 |
-
json.dumps(
|
111 |
-
{
|
112 |
-
"model": MODEL_NAME,
|
113 |
-
"created_at": datetime.now(timezone.utc).strftime(
|
114 |
-
"%Y-%m-%dT%H:%M:%S.%fZ"
|
115 |
-
),
|
116 |
-
"message": {
|
117 |
-
"role": "assistant",
|
118 |
-
"content": chunk,
|
119 |
-
},
|
120 |
-
"done": False,
|
121 |
-
},
|
122 |
-
ensure_ascii=False,
|
123 |
-
).encode("utf-8")
|
124 |
-
+ b"\n"
|
125 |
-
) # the b"\n" is important
|
126 |
-
|
127 |
-
return StreamingResponse(ollama_resp(resp), media_type="application/json")
|
128 |
-
else:
|
129 |
-
return resp
|
130 |
-
|
131 |
-
|
132 |
-
@app.get("/health")
|
133 |
-
async def health_check():
|
134 |
-
return {"status": "healthy"}
|
135 |
-
|
136 |
-
|
137 |
-
if __name__ == "__main__":
|
138 |
-
import uvicorn
|
139 |
-
|
140 |
-
uvicorn.run(app, host="0.0.0.0", port=8020)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|