iammartian0 commited on
Commit
3c3ef32
·
verified ·
1 Parent(s): 2b43eec

Delete agent.py

Browse files
Files changed (1) hide show
  1. agent.py +0 -98
agent.py DELETED
@@ -1,98 +0,0 @@
1
- import os
2
- from dotenv import load_dotenv
3
- from tavily import TavilyClient
4
- from cerebras.cloud.sdk import Cerebras
5
-
6
- load_dotenv()
7
-
8
- SAMPLE_MODEL = "gpt-oss-120b"
9
-
10
- class BasicAgent:
11
- def __init__(self):
12
- print("--- Initializing BasicAgent ---")
13
-
14
- # 1. Fetch Secrets
15
- self.hf_token = os.getenv("HF_TOKEN")
16
- self.tavily_key = os.getenv("TAVILY_API_KEY")
17
- self.cerebras_key = os.getenv("CEREBRAS_API_KEY")
18
-
19
- # 2. Check for missing keys and Raise explicit errors
20
- if not self.hf_token:
21
- raise ValueError("❌ Secret 'HF_TOKEN' is missing. Please add it in Space Settings.")
22
-
23
- if not self.tavily_key:
24
- raise ValueError("❌ Secret 'TAVILY_API_KEY' is missing. Please add it in Space Settings.")
25
-
26
- if not self.cerebras_key:
27
- raise ValueError("❌ Secret 'CEREBRAS_API_KEY' is missing. Please add it in Space Settings.")
28
-
29
- # 3. Initialize Clients
30
- try:
31
- self.tavily = TavilyClient(api_key=self.tavily_key)
32
- self.llm_client = Cerebras(api_key=self.cerebras_key)
33
- except Exception as e:
34
- raise RuntimeError(f"❌ Failed to initialize external libraries: {e}")
35
-
36
- self.model = os.getenv("LLM_MODEL", SAMPLE_MODEL)
37
- print("✅ BasicAgent initialized successfully.")
38
- def _truncate_query(self, query: str, max_len: int = 390) -> str:
39
- return query[:max_len] + ("..." if len(query) > max_len else "")
40
-
41
-
42
- def answer(self, question: str, mode="context") -> str:
43
- # Truncate BEFORE Tavily call
44
- truncated_question = self._truncate_query(question)
45
-
46
- # Use truncated_question for Tavily
47
- context = (
48
- self.tavily.get_search_context(query=truncated_question)
49
- if mode == "context"
50
- else self.tavily.qna_search(query=truncated_question)
51
- )
52
-
53
- if not context:
54
- context = "No context found. Answer based on your knowledge."
55
-
56
- # Use truncated_question for LLM too
57
- messages = [
58
- {
59
- "role": "system",
60
- "content": (
61
- "You are a precise data extraction engine. "
62
- "Your task is to provide ONLY the exact answer to the user's question based on the context. "
63
- "Do not provide explanations, introductory text, or conversational filler. "
64
- "Do not say 'The answer is' or 'Based on the context'. "
65
- "If the answer is a name, number, or date, return JUST that specific value."
66
- )
67
- },
68
- {
69
- "role": "user",
70
- "content": f"Context:\n{context}\n\nQuestion: {truncated_question}\n\nExact Answer:"
71
- }
72
- ]
73
-
74
- comp = self.llm_client.chat.completions.create(
75
- model=self.model,
76
- messages=messages,
77
- temperature=0.0, # 0.0 makes the model very strict and factual
78
- max_tokens=100
79
- )
80
-
81
- return comp.choices[0].message.content.strip()
82
-
83
-
84
- def __call__(self, question: str) -> str:
85
- return self.answer(question)
86
-
87
-
88
- if __name__ == "__main__":
89
- agent = BasicAgent()
90
-
91
- # Default (context mode)
92
- print(agent("Who founded Tesla?"))
93
-
94
- # Quick Q&A mode
95
- print(agent("What is the capital of France?", mode="qna"))
96
-
97
- # Return both context and answer
98
- print(agent("Explain Burning Man floods", return_context=True))