fabianonbfilho commited on
Commit
6d8dc9e
·
verified ·
1 Parent(s): c6cdf51

Upload src/labdaps/chat/session.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. src/labdaps/chat/session.py +16 -13
src/labdaps/chat/session.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  from dataclasses import dataclass
3
- import google.generativeai as genai
4
- from src.labdaps.config import GEMINI_MODEL, MAX_HISTORY_MESSAGES
5
  from src.labdaps.retrieval.retriever import RetrievedChunk, retrieve
6
  from src.labdaps.ingestion.embedder import Embedder
7
  from src.labdaps.chat.prompts import build_system_prompt
@@ -16,8 +16,7 @@ class ChatResponse:
16
  class ChatSession:
17
  def __init__(self, embedder: Embedder):
18
  self.embedder = embedder
19
- genai.configure(api_key=os.environ["GEMINI_API_KEY"])
20
- self.model = genai.GenerativeModel(GEMINI_MODEL)
21
  self.history: list[dict] = []
22
 
23
  def reset(self):
@@ -28,17 +27,21 @@ class ChatSession:
28
  system_prompt = build_system_prompt(chunks)
29
 
30
  trimmed_history = self.history[-MAX_HISTORY_MESSAGES:]
31
- gemini_history = [
32
- {"role": "model" if m["role"] == "assistant" else "user", "parts": [m["content"]]}
33
- for m in trimmed_history
34
- ]
35
-
36
- chat = self.model.start_chat(history=gemini_history)
37
- full_prompt = f"{system_prompt}\n\n---\n\nPergunta: {question}"
38
 
39
  full_text = ""
40
- for chunk in chat.send_message(full_prompt, stream=True):
41
- delta = chunk.text or ""
 
 
 
 
 
 
42
  full_text += delta
43
  yield delta, chunks
44
 
 
1
  import os
2
  from dataclasses import dataclass
3
+ from groq import Groq
4
+ from src.labdaps.config import GROQ_MODEL, MAX_HISTORY_MESSAGES
5
  from src.labdaps.retrieval.retriever import RetrievedChunk, retrieve
6
  from src.labdaps.ingestion.embedder import Embedder
7
  from src.labdaps.chat.prompts import build_system_prompt
 
16
  class ChatSession:
17
  def __init__(self, embedder: Embedder):
18
  self.embedder = embedder
19
+ self.client = Groq(api_key=os.environ["GROQ_API_KEY"])
 
20
  self.history: list[dict] = []
21
 
22
  def reset(self):
 
27
  system_prompt = build_system_prompt(chunks)
28
 
29
  trimmed_history = self.history[-MAX_HISTORY_MESSAGES:]
30
+ messages = (
31
+ [{"role": "system", "content": system_prompt}]
32
+ + trimmed_history
33
+ + [{"role": "user", "content": question}]
34
+ )
 
 
35
 
36
  full_text = ""
37
+ stream = self.client.chat.completions.create(
38
+ model=GROQ_MODEL,
39
+ messages=messages,
40
+ stream=True,
41
+ max_tokens=2048,
42
+ )
43
+ for chunk in stream:
44
+ delta = chunk.choices[0].delta.content or ""
45
  full_text += delta
46
  yield delta, chunks
47