ErdemTheFixer commited on
Commit
0cd0932
·
verified ·
1 Parent(s): 6495d7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -27
app.py CHANGED
@@ -77,61 +77,64 @@ class WikipediaTool(Tool):
77
  return f"Wikipedia error: {e}"
78
 
79
  # --- Basic Agent Definition ---
80
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
81
  class BasicAgent:
82
  def __init__(self):
83
  print("BasicAgent initialized.")
84
 
85
- # Prefer OpenRouter, then Together, else fallback to OpenAI (if key exists)
 
86
  openrouter_key = os.getenv("OPENROUTER_API_KEY")
87
  together_key = os.getenv("TOGETHER_API_KEY")
88
- openai_key = os.getenv("OPENAI_API_KEY")
89
 
90
- if openrouter_key:
91
- # Configure OpenAIServerModel to use OpenRouter
 
92
  os.environ["OPENAI_API_KEY"] = openrouter_key
93
  os.environ["OPENAI_API_BASE"] = "https://openrouter.ai/api/v1"
94
  os.environ["OPENAI_BASE_URL"] = "https://openrouter.ai/api/v1"
95
- model_id = os.getenv("OPENROUTER_MODEL", "meta-llama/llama-3.1-8b-instruct:free")
96
  provider = "openrouter"
97
- elif together_key:
98
- # Configure OpenAIServerModel to use Together.ai
99
  os.environ["OPENAI_API_KEY"] = together_key
100
  os.environ["OPENAI_API_BASE"] = "https://api.together.xyz/v1"
101
  os.environ["OPENAI_BASE_URL"] = "https://api.together.xyz/v1"
102
- model_id = os.getenv("TOGETHER_MODEL", "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo")
103
  provider = "together"
104
- elif openai_key:
105
- # Fallback to OpenAI if still available
106
- os.environ.setdefault("OPENAI_API_BASE", "https://api.openai.com/v1")
107
- os.environ.setdefault("OPENAI_BASE_URL", "https://api.openai.com/v1")
108
- model_id = os.getenv("OPENAI_MODEL", "gpt-4o-mini")
109
- provider = "openai"
110
  else:
111
- raise ValueError("No API key found. Set one of: OPENROUTER_API_KEY, TOGETHER_API_KEY, or OPENAI_API_KEY.")
 
 
 
 
 
 
112
 
113
  print(f"Using provider={provider}, model_id={model_id}")
114
-
115
- # Initialize the model routed via OpenAI-compatible server
116
  model = OpenAIServerModel(model_id=model_id)
117
 
118
- # Tools
119
  search_tool = DuckDuckGoSearchTool()
120
  wiki_top_k = int(os.getenv("WIKI_TOP_K", "3"))
121
  wiki_lang = os.getenv("WIKI_LANG", "en")
122
  wikipedia_tool = WikipediaTool(top_k=wiki_top_k, language=wiki_lang)
123
 
124
- # Agent
125
- self.agent = CodeAgent(
126
- model=model,
127
- tools=[search_tool, wikipedia_tool]
128
- )
129
 
130
  def __call__(self, question: str) -> str:
131
  print(f"Agent received question (first 50 chars): {question[:50]}...")
132
- answer = self.agent.run(question)
133
- print(f"Agent returning fixed answer: {answer}")
134
- return answer
 
 
 
 
 
 
 
 
 
 
135
 
136
  def run_and_submit_all( profile: gr.OAuthProfile | None):
137
  """
 
77
  return f"Wikipedia error: {e}"
78
 
79
  # --- Basic Agent Definition ---
 
80
  class BasicAgent:
81
  def __init__(self):
82
  print("BasicAgent initialized.")
83
 
84
+ # Provider selection (force away from OpenAI if quota-exceeded)
85
+ force = (os.getenv("FORCE_PROVIDER") or "").strip().lower()
86
  openrouter_key = os.getenv("OPENROUTER_API_KEY")
87
  together_key = os.getenv("TOGETHER_API_KEY")
 
88
 
89
+ provider = None
90
+ model_id = None
91
+ if force == "openrouter" or (openrouter_key and force != "together"):
92
  os.environ["OPENAI_API_KEY"] = openrouter_key
93
  os.environ["OPENAI_API_BASE"] = "https://openrouter.ai/api/v1"
94
  os.environ["OPENAI_BASE_URL"] = "https://openrouter.ai/api/v1"
 
95
  provider = "openrouter"
96
+ model_id = os.getenv("OPENROUTER_MODEL", "meta-llama/Meta-Llama-3-8B-Instruct")
97
+ elif force == "together" or together_key:
98
  os.environ["OPENAI_API_KEY"] = together_key
99
  os.environ["OPENAI_API_BASE"] = "https://api.together.xyz/v1"
100
  os.environ["OPENAI_BASE_URL"] = "https://api.together.xyz/v1"
 
101
  provider = "together"
102
+ model_id = os.getenv("TOGETHER_MODEL", "meta-llama/Meta-Llama-3-8B-Instruct")
 
 
 
 
 
103
  else:
104
+ # No non-OpenAI key available → use Wikipedia-only fallback
105
+ self.wikipedia_only = True
106
+ wiki_top_k = int(os.getenv("WIKI_TOP_K", "3"))
107
+ wiki_lang = os.getenv("WIKI_LANG", "en")
108
+ self.wikipedia_tool = WikipediaTool(top_k=wiki_top_k, language=wiki_lang)
109
+ print("No non-OpenAI key set; running in Wikipedia-only mode.")
110
+ return
111
 
112
  print(f"Using provider={provider}, model_id={model_id}")
 
 
113
  model = OpenAIServerModel(model_id=model_id)
114
 
 
115
  search_tool = DuckDuckGoSearchTool()
116
  wiki_top_k = int(os.getenv("WIKI_TOP_K", "3"))
117
  wiki_lang = os.getenv("WIKI_LANG", "en")
118
  wikipedia_tool = WikipediaTool(top_k=wiki_top_k, language=wiki_lang)
119
 
120
+ self.agent = CodeAgent(model=model, tools=[search_tool, wikipedia_tool])
121
+ self.wikipedia_only = False
 
 
 
122
 
123
  def __call__(self, question: str) -> str:
124
  print(f"Agent received question (first 50 chars): {question[:50]}...")
125
+ if self.wikipedia_only:
126
+ # Direct Wikipedia fallback
127
+ return self.wikipedia_tool.forward(question)
128
+
129
+ try:
130
+ return self.agent.run(question)
131
+ except Exception as e:
132
+ # If provider throws (e.g., 429/insufficient_quota), fall back to Wikipedia
133
+ print(f"LLM error, falling back to Wikipedia: {e}")
134
+ try:
135
+ return self.agent.tools[1].forward(question) # WikipediaTool as second tool
136
+ except Exception:
137
+ return "I cannot determine the answer."
138
 
139
  def run_and_submit_all( profile: gr.OAuthProfile | None):
140
  """