rsrini7 commited on
Commit
ebb3b12
·
1 Parent(s): b1006c1

added openrouter api and called using simple http layer

Browse files
Files changed (3) hide show
  1. app.py +24 -14
  2. global_config.py +18 -2
  3. helpers/llm_helper.py +35 -0
app.py CHANGED
@@ -345,20 +345,30 @@ def set_up_chat_ui():
345
  )
346
  return
347
 
348
- for chunk in llm.stream(formatted_template):
349
- if isinstance(chunk, str):
350
- response += chunk
351
- else:
352
- response += chunk.content # AIMessageChunk
353
-
354
- # Update the progress bar with an approx progress percentage
355
- progress_bar.progress(
356
- min(
357
- len(response) / gcfg.get_max_output_tokens(llm_provider_to_use),
358
- 0.95
359
- ),
360
- text='Streaming content...this might take a while...'
361
- )
 
 
 
 
 
 
 
 
 
 
362
  except (httpx.ConnectError, requests.exceptions.ConnectionError):
363
  handle_error(
364
  'A connection error occurred while streaming content from the LLM endpoint.'
 
345
  )
346
  return
347
 
348
+ if provider == GlobalConfig.PROVIDER_OPENROUTER:
349
+ # OpenRouter returns a function, not a LangChain LLM. Call it directly.
350
+ response_json = llm(formatted_template)
351
+ # Extract the text from the OpenAI-compatible response
352
+ try:
353
+ response = response_json["choices"][0]["message"]["content"]
354
+ except Exception as ex:
355
+ handle_error(f"Failed to parse OpenRouter response: {ex}\nRaw response: {response_json}", True)
356
+ return
357
+ else:
358
+ for chunk in llm.stream(formatted_template):
359
+ if isinstance(chunk, str):
360
+ response += chunk
361
+ else:
362
+ response += chunk.content # AIMessageChunk
363
+
364
+ # Update the progress bar with an approx progress percentage
365
+ progress_bar.progress(
366
+ min(
367
+ len(response) / gcfg.get_max_output_tokens(llm_provider_to_use),
368
+ 0.95
369
+ ),
370
+ text='Streaming content...this might take a while...'
371
+ )
372
  except (httpx.ConnectError, requests.exceptions.ConnectionError):
373
  handle_error(
374
  'A connection error occurred while streaming content from the LLM endpoint.'
global_config.py CHANGED
@@ -23,6 +23,7 @@ class GlobalConfig:
23
  PROVIDER_OLLAMA = 'ol'
24
  PROVIDER_TOGETHER_AI = 'to'
25
  PROVIDER_AZURE_OPENAI = 'az'
 
26
  VALID_PROVIDERS = {
27
  PROVIDER_COHERE,
28
  PROVIDER_GOOGLE_GEMINI,
@@ -30,6 +31,7 @@ class GlobalConfig:
30
  PROVIDER_OLLAMA,
31
  PROVIDER_TOGETHER_AI,
32
  PROVIDER_AZURE_OPENAI,
 
33
  }
34
  VALID_MODELS = {
35
  '[az]azure/open-ai': {
@@ -72,6 +74,19 @@ class GlobalConfig:
72
  'max_new_tokens': 4096,
73
  'paid': True,
74
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  }
76
  LLM_PROVIDER_HELP = (
77
  'LLM provider codes:\n\n'
@@ -79,13 +94,14 @@ class GlobalConfig:
79
  '- **[co]**: Cohere\n'
80
  '- **[gg]**: Google Gemini API\n'
81
  '- **[hf]**: Hugging Face Inference API\n'
82
- '- **[to]**: Together AI\n\n'
 
83
  '[Find out more](https://github.com/barun-saha/slide-deck-ai?tab=readme-ov-file#summary-of-the-llms)'
84
  )
85
  DEFAULT_MODEL_INDEX = int(os.environ.get('DEFAULT_MODEL_INDEX', '4'))
86
  LLM_MODEL_TEMPERATURE = 0.2
87
  LLM_MODEL_MIN_OUTPUT_LENGTH = 100
88
- LLM_MODEL_MAX_INPUT_LENGTH = 400 # characters
89
  MAX_PAGE_COUNT = 50
90
 
91
  LOG_LEVEL = 'DEBUG'
 
23
  PROVIDER_OLLAMA = 'ol'
24
  PROVIDER_TOGETHER_AI = 'to'
25
  PROVIDER_AZURE_OPENAI = 'az'
26
+ PROVIDER_OPENROUTER = 'or'
27
  VALID_PROVIDERS = {
28
  PROVIDER_COHERE,
29
  PROVIDER_GOOGLE_GEMINI,
 
31
  PROVIDER_OLLAMA,
32
  PROVIDER_TOGETHER_AI,
33
  PROVIDER_AZURE_OPENAI,
34
+ PROVIDER_OPENROUTER,
35
  }
36
  VALID_MODELS = {
37
  '[az]azure/open-ai': {
 
74
  'max_new_tokens': 4096,
75
  'paid': True,
76
  },
77
+ '[or]openai/gpt-3.5-turbo': {
78
+ 'description': 'OpenAI GPT-3.5 Turbo (via OpenRouter)',
79
+ 'max_new_tokens': 2048,
80
+ },
81
+ '[or]openrouter/gpt-4-omni': {
82
+ 'description': 'OpenRouter GPT-4 Omni',
83
+ 'max_new_tokens': 8192,
84
+ 'paid': True,
85
+ },
86
+ '[or]openrouter/mixtral-8x22b-instruct': {
87
+ 'description': 'Mixtral 8x22B Instruct (via OpenRouter)',
88
+ 'max_new_tokens': 2048,
89
+ },
90
  }
91
  LLM_PROVIDER_HELP = (
92
  'LLM provider codes:\n\n'
 
94
  '- **[co]**: Cohere\n'
95
  '- **[gg]**: Google Gemini API\n'
96
  '- **[hf]**: Hugging Face Inference API\n'
97
+ '- **[to]**: Together AI\n'
98
+ '- **[or]**: OpenRouter\n\n'
99
  '[Find out more](https://github.com/barun-saha/slide-deck-ai?tab=readme-ov-file#summary-of-the-llms)'
100
  )
101
  DEFAULT_MODEL_INDEX = int(os.environ.get('DEFAULT_MODEL_INDEX', '4'))
102
  LLM_MODEL_TEMPERATURE = 0.2
103
  LLM_MODEL_MIN_OUTPUT_LENGTH = 100
104
+ LLM_MODEL_MAX_INPUT_LENGTH = 10000 # characters
105
  MAX_PAGE_COUNT = 50
106
 
107
  LOG_LEVEL = 'DEBUG'
helpers/llm_helper.py CHANGED
@@ -188,6 +188,41 @@ def get_langchain_llm(
188
  api_key=api_key,
189
  )
190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
  if provider == GlobalConfig.PROVIDER_COHERE:
192
  from langchain_cohere.llms import Cohere
193
 
 
188
  api_key=api_key,
189
  )
190
 
191
+ if provider == GlobalConfig.PROVIDER_OPENROUTER:
192
+ logger.debug('Getting LLM via OpenRouter: %s', model)
193
+ OPENROUTER_API_URL = "https://openrouter.ai/api/v1/chat/completions"
194
+ OPENROUTER_API_KEY = api_key
195
+ import os
196
+ import requests
197
+
198
+ def openrouter_completion(prompt, model=model, api_key=OPENROUTER_API_KEY):
199
+ headers = {
200
+ "Authorization": f"Bearer {api_key}",
201
+ "Content-Type": "application/json",
202
+ }
203
+ # Optionally add analytics headers if available
204
+ site_url = os.getenv("OPENROUTER_SITE_URL")
205
+ app_name = os.getenv("OPENROUTER_SITE_NAME")
206
+ if site_url:
207
+ headers["HTTP-Referer"] = site_url
208
+ if app_name:
209
+ headers["X-Title"] = app_name
210
+ data = {
211
+ "model": model,
212
+ "messages": [
213
+ {"role": "system", "content": "You are a helpful assistant summarizing technical support information. Provide a concise summary or key action points based on the provided context."},
214
+ {"role": "user", "content": prompt},
215
+ ]
216
+ }
217
+ response = requests.post(
218
+ url=OPENROUTER_API_URL,
219
+ headers=headers,
220
+ json=data
221
+ )
222
+ response.raise_for_status()
223
+ return response.json()
224
+ return openrouter_completion
225
+
226
  if provider == GlobalConfig.PROVIDER_COHERE:
227
  from langchain_cohere.llms import Cohere
228