Bahodir Nematjonov commited on
Commit
8b883c8
·
1 Parent(s): e4f5d4a
Files changed (2) hide show
  1. main.py +5 -9
  2. utils.py +14 -12
main.py CHANGED
@@ -1,10 +1,10 @@
1
  from fastapi import FastAPI, Depends, HTTPException, status
2
- from fastapi.responses import FileResponse
3
  from fastapi.staticfiles import StaticFiles
4
  from jose import JWTError
5
  from schemas import UserRegister, TokenResponse, RefreshTokenRequest, QueryInput
6
  from auth import register_user, get_db, authenticate_user, create_token, verify_token, verify_access_token, Session
7
- from utils import search_with_llm
8
  from fastapi.security import OAuth2PasswordRequestForm
9
  from pathlib import Path
10
  from datetime import timedelta
@@ -21,6 +21,8 @@ REFRESH_TOKEN_EXPIRE_DAYS = 7
21
 
22
  app = FastAPI()
23
 
 
 
24
  # Entry Endpoint
25
  @app.get('/')
26
  def index() -> FileResponse:
@@ -95,18 +97,12 @@ async def refresh(refresh_request: RefreshTokenRequest):
95
  headers={"WWW-Authenticate": "Bearer"},
96
  )
97
 
98
-
99
-
100
  @app.post("/search")
101
  async def search(
102
  query_input: QueryInput,
103
  username: str = Depends(verify_access_token),
104
  ):
105
- try:
106
- response = search_with_llm(query_input.query)
107
- return {"response": response}
108
- except Exception as e:
109
- raise HTTPException(status_code=500, detail=f"Search failed: {str(e)}")
110
 
111
  # WebSocket endpoint for streaming
112
  @app.on_event("startup")
 
1
  from fastapi import FastAPI, Depends, HTTPException, status
2
+ from fastapi.responses import FileResponse, StreamingResponse
3
  from fastapi.staticfiles import StaticFiles
4
  from jose import JWTError
5
  from schemas import UserRegister, TokenResponse, RefreshTokenRequest, QueryInput
6
  from auth import register_user, get_db, authenticate_user, create_token, verify_token, verify_access_token, Session
7
+ from utils import generate_stream
8
  from fastapi.security import OAuth2PasswordRequestForm
9
  from pathlib import Path
10
  from datetime import timedelta
 
21
 
22
  app = FastAPI()
23
 
24
+
25
+
26
  # Entry Endpoint
27
  @app.get('/')
28
  def index() -> FileResponse:
 
97
  headers={"WWW-Authenticate": "Bearer"},
98
  )
99
 
 
 
100
  @app.post("/search")
101
  async def search(
102
  query_input: QueryInput,
103
  username: str = Depends(verify_access_token),
104
  ):
105
+ return StreamingResponse(generate_stream(query_input.query), media_type="text/plain")
 
 
 
 
106
 
107
  # WebSocket endpoint for streaming
108
  @app.on_event("startup")
utils.py CHANGED
@@ -1,21 +1,23 @@
1
- import os
2
  import ollama
3
- from typing import List, Dict
4
 
5
  def cosine_similarity(embedding_0, embedding_1):
6
  pass
7
 
8
-
9
  def generate_embedding(model, text: str, model_type: str) -> List[float]:
10
  pass
11
 
12
- def search_with_llm(query: str, model: str = "llama3.2"):
13
- try:
14
- response = ollama.chat(
15
- model=model,
16
- messages=[{"role": "user", "content": query}]
17
- )
18
- return response["message"]["content"]
19
- except Exception as e:
20
- return f"❌ Error processing request: {str(e)}"
 
 
 
21
 
 
1
+ import asyncio
2
  import ollama
3
+ from typing import List
4
 
5
  def cosine_similarity(embedding_0, embedding_1):
6
  pass
7
 
 
8
  def generate_embedding(model, text: str, model_type: str) -> List[float]:
9
  pass
10
 
11
+ async def generate_stream(query: str):
12
+ """Stream responses from Ollama in real-time."""
13
+ stream = ollama.chat(
14
+ model="llama3.2", # Choose your model (mistral, llama2, gemma)
15
+ messages=[{"role": "user", "content": query}],
16
+ stream=True # Enable streaming
17
+ )
18
+
19
+ for chunk in stream:
20
+ if "message" in chunk and "content" in chunk["message"]:
21
+ yield chunk["message"]["content"]
22
+ await asyncio.sleep(0) # Allow async executi
23