Spaces:
Sleeping
Sleeping
File size: 1,505 Bytes
18d4fb7 6d8dc9e cba026c 6d8dc9e cba026c 18d4fb7 cba026c 6d8dc9e cba026c 6d8dc9e 18d4fb7 cba026c | 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 | import os
from dataclasses import dataclass
from groq import Groq
from src.labdaps.config import GROQ_MODEL, MAX_HISTORY_MESSAGES
from src.labdaps.retrieval.retriever import RetrievedChunk, retrieve
from src.labdaps.ingestion.embedder import Embedder
from src.labdaps.chat.prompts import build_system_prompt
@dataclass
class ChatResponse:
text: str
sources: list[RetrievedChunk]
class ChatSession:
def __init__(self, embedder: Embedder):
self.embedder = embedder
self.client = Groq(api_key=os.environ["GROQ_API_KEY"])
self.history: list[dict] = []
def reset(self):
self.history = []
def ask(self, question: str):
chunks = retrieve(question, self.embedder)
system_prompt = build_system_prompt(chunks)
trimmed_history = self.history[-MAX_HISTORY_MESSAGES:]
messages = (
[{"role": "system", "content": system_prompt}]
+ trimmed_history
+ [{"role": "user", "content": question}]
)
full_text = ""
stream = self.client.chat.completions.create(
model=GROQ_MODEL,
messages=messages,
stream=True,
max_tokens=2048,
)
for chunk in stream:
delta = chunk.choices[0].delta.content or ""
full_text += delta
yield delta, chunks
self.history.append({"role": "user", "content": question})
self.history.append({"role": "assistant", "content": full_text})
|