Spaces:
Sleeping
Sleeping
Commit
·
e8c5d46
1
Parent(s):
785b85c
New approach
Browse files- __pycache__/app.cpython-310.pyc +0 -0
- app.py +122 -108
- requirements.txt +2 -1
__pycache__/app.cpython-310.pyc
DELETED
Binary file (3.14 kB)
|
|
app.py
CHANGED
@@ -1,119 +1,133 @@
|
|
1 |
-
import os
|
2 |
-
import logging
|
3 |
-
import requests
|
4 |
-
from fastapi import FastAPI, HTTPException
|
5 |
-
from fastapi.responses import StreamingResponse
|
6 |
-
from pydantic import BaseModel
|
7 |
-
from openai import OpenAI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
logger = logging.getLogger(__name__)
|
11 |
|
12 |
-
app
|
13 |
-
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
|
17 |
|
18 |
-
#
|
19 |
-
#
|
20 |
-
#
|
21 |
-
#
|
22 |
-
#
|
23 |
-
|
24 |
-
#
|
25 |
-
#
|
26 |
-
#
|
27 |
-
#
|
28 |
-
#
|
29 |
-
# )
|
30 |
-
# return completion
|
31 |
|
32 |
-
|
33 |
-
text: str
|
34 |
|
35 |
-
@app.
|
36 |
-
def
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
|
|
39 |
|
40 |
-
#
|
41 |
-
#
|
42 |
-
#
|
43 |
# try:
|
44 |
-
# response =
|
45 |
-
#
|
|
|
|
|
46 |
# except Exception as e:
|
47 |
-
#
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
response =
|
74 |
-
|
75 |
-
json={"model": model, "prompt": prompt, "system": system, "stream": stream}
|
76 |
-
)
|
77 |
-
except Exception as e:
|
78 |
-
return {"error": str(e)}
|
79 |
-
# print(response)
|
80 |
-
|
81 |
-
return response.json()
|
82 |
-
|
83 |
-
@app.post("/embed")
|
84 |
-
async def get_embedding(model: str, text: str):
|
85 |
-
"""Generate embeddings for the given text using a model."""
|
86 |
-
try:
|
87 |
-
response = requests.post(
|
88 |
-
f"{OLLAMA_URL}/api/embeddings",
|
89 |
-
json={"model": model, "prompt": text}
|
90 |
-
)
|
91 |
-
except Exception as e:
|
92 |
-
return {"error": str(e)}
|
93 |
-
# print(response)
|
94 |
-
|
95 |
-
return response.json()
|
96 |
-
|
97 |
-
@app.post("/chat")
|
98 |
-
async def chat(model: str, message: str, system: str = "You are a helpful chatbot."):
|
99 |
-
"""Chat with the model while maintaining context."""
|
100 |
-
try:
|
101 |
-
response = requests.post(
|
102 |
-
f"{OLLAMA_URL}/api/chat",
|
103 |
-
json={"model": model, "messages": [{"role": "system", "content": system}, {"role": "user", "content": message}]}
|
104 |
-
)
|
105 |
-
except Exception as e:
|
106 |
-
return {"error": str(e)}
|
107 |
-
# print(response)
|
108 |
-
|
109 |
-
return response.json()
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
@app.on_event("startup")
|
114 |
-
async def startup_event():
|
115 |
-
logger.info(f"Starting up with model: {MODEL_NAME}")
|
116 |
-
|
117 |
-
@app.on_event("shutdown")
|
118 |
-
async def shutdown_event():
|
119 |
-
logger.info("Shutting down")
|
|
|
1 |
+
# import os
|
2 |
+
# import logging
|
3 |
+
# import requests
|
4 |
+
# from fastapi import FastAPI, HTTPException
|
5 |
+
# from fastapi.responses import StreamingResponse
|
6 |
+
# from pydantic import BaseModel
|
7 |
+
# from openai import OpenAI
|
8 |
+
|
9 |
+
# logging.basicConfig(level=logging.INFO)
|
10 |
+
# logger = logging.getLogger(__name__)
|
11 |
+
|
12 |
+
# app = FastAPI()
|
13 |
+
# MODEL_NAME = 'llama3.1:8b'
|
14 |
+
|
15 |
+
# OLLAMA_URL = "http://localhost:11434"
|
16 |
+
|
17 |
+
|
18 |
+
# # def create_model(query):
|
19 |
+
# # message = [
|
20 |
+
# # {"role": "system", "content": "You are a general chat bot."},
|
21 |
+
# # {"role": "user", "content": f"{query}"}
|
22 |
+
# # ]
|
23 |
+
|
24 |
+
# # completion = ollama_client.chat.completions.create(
|
25 |
+
# # model="llama3.1:8b",
|
26 |
+
# # messages=message
|
27 |
+
# # # response_format=base_model,
|
28 |
+
# # #temperature = 0.1
|
29 |
+
# # )
|
30 |
+
# # return completion
|
31 |
+
|
32 |
+
# class Question(BaseModel):
|
33 |
+
# text: str
|
34 |
+
|
35 |
+
# @app.get("/")
|
36 |
+
# def read_root():
|
37 |
+
# return {"Hello": f"Welcome to {MODEL_NAME} FastAPI"}
|
38 |
+
|
39 |
+
|
40 |
+
# # # POST endpoint to query the LLM
|
41 |
+
# # @app.post("/ask")
|
42 |
+
# # async def ask_question(question: Question):
|
43 |
+
# # try:
|
44 |
+
# # response = create_model(question.text)
|
45 |
+
# # return {"response": response}
|
46 |
+
# # except Exception as e:
|
47 |
+
# # raise HTTPException(status_code=500, detail=f"Error querying the model: {str(e)}")
|
48 |
+
|
49 |
+
|
50 |
+
# @app.get("/list_models")
|
51 |
+
# async def list_models():
|
52 |
+
# """List all available models in Ollama."""
|
53 |
+
# try:
|
54 |
+
# response = requests.get(f"{OLLAMA_URL}/api/tags")
|
55 |
+
# except Exception as e:
|
56 |
+
# return {"error": str(e)}
|
57 |
|
58 |
+
# return response.json()
|
|
|
59 |
|
60 |
+
# @app.post("/pull_model")
|
61 |
+
# async def pull_model(model_name: str):
|
62 |
+
# """Pull a model from Ollama's repository."""
|
63 |
+
# response = requests.post(f"{OLLAMA_URL}/api/pull", json={"name": model_name})
|
64 |
+
# # print(response)
|
65 |
|
66 |
+
# return response.json()
|
67 |
|
68 |
|
69 |
+
# @app.post("/generate")
|
70 |
+
# async def generate_text(model: str, prompt: str, system: str = "You are a helpful AI assistant.", stream: bool = False):
|
71 |
+
# """Generate text from a given prompt using a specific model."""
|
72 |
+
# try:
|
73 |
+
# response = requests.post(
|
74 |
+
# f"{OLLAMA_URL}/api/generate",
|
75 |
+
# json={"model": model, "prompt": prompt, "system": system, "stream": stream}
|
76 |
+
# )
|
77 |
+
# except Exception as e:
|
78 |
+
# return {"error": str(e)}
|
79 |
+
# # print(response)
|
|
|
|
|
80 |
|
81 |
+
# return response.json()
|
|
|
82 |
|
83 |
+
# @app.post("/embed")
|
84 |
+
# async def get_embedding(model: str, text: str):
|
85 |
+
# """Generate embeddings for the given text using a model."""
|
86 |
+
# try:
|
87 |
+
# response = requests.post(
|
88 |
+
# f"{OLLAMA_URL}/api/embeddings",
|
89 |
+
# json={"model": model, "prompt": text}
|
90 |
+
# )
|
91 |
+
# except Exception as e:
|
92 |
+
# return {"error": str(e)}
|
93 |
+
# # print(response)
|
94 |
|
95 |
+
# return response.json()
|
96 |
|
97 |
+
# @app.post("/chat")
|
98 |
+
# async def chat(model: str, message: str, system: str = "You are a helpful chatbot."):
|
99 |
+
# """Chat with the model while maintaining context."""
|
100 |
# try:
|
101 |
+
# response = requests.post(
|
102 |
+
# f"{OLLAMA_URL}/api/chat",
|
103 |
+
# json={"model": model, "messages": [{"role": "system", "content": system}, {"role": "user", "content": message}]}
|
104 |
+
# )
|
105 |
# except Exception as e:
|
106 |
+
# return {"error": str(e)}
|
107 |
+
# # print(response)
|
108 |
+
|
109 |
+
# return response.json()
|
110 |
+
|
111 |
+
|
112 |
+
|
113 |
+
# @app.on_event("startup")
|
114 |
+
# async def startup_event():
|
115 |
+
# logger.info(f"Starting up with model: {MODEL_NAME}")
|
116 |
+
|
117 |
+
# @app.on_event("shutdown")
|
118 |
+
# async def shutdown_event():
|
119 |
+
# logger.info("Shutting down")
|
120 |
+
|
121 |
+
|
122 |
+
|
123 |
+
from fastapi import FastAPI
|
124 |
+
import httpx
|
125 |
+
|
126 |
+
app = FastAPI()
|
127 |
+
TARGET_SERVER = "http://localhost:11434"
|
128 |
+
|
129 |
+
@app.get("/proxy/{path:path}")
|
130 |
+
async def proxy_request(path: str):
|
131 |
+
async with httpx.AsyncClient() as client:
|
132 |
+
response = await client.get(f"{TARGET_SERVER}/{path}")
|
133 |
+
return response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -3,4 +3,5 @@ uvicorn
|
|
3 |
ollama
|
4 |
openai
|
5 |
pydantic
|
6 |
-
requests
|
|
|
|
3 |
ollama
|
4 |
openai
|
5 |
pydantic
|
6 |
+
requests
|
7 |
+
httpx
|