Simon Strandgaard
commited on
Commit
·
5d19961
1
Parent(s):
0bca3d3
Obtain the OPENROUTER_API_KEY from huggingface
Browse files- app.py +18 -2
- src/llm_factory.py +11 -2
app.py
CHANGED
@@ -1,8 +1,24 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
-
import subprocess
|
4 |
from src.prompt.prompt_catalog import PromptCatalog
|
5 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
if True:
|
8 |
# Load modules from src
|
|
|
1 |
+
import os
|
2 |
+
import subprocess
|
3 |
import gradio as gr
|
4 |
from huggingface_hub import InferenceClient
|
|
|
5 |
from src.prompt.prompt_catalog import PromptCatalog
|
6 |
+
from src.llm_factory import get_llm
|
7 |
+
from llama_index.core.llms import ChatMessage
|
8 |
+
|
9 |
+
if True:
|
10 |
+
llm = get_llm("openrouter-paid-gemini-2.0-flash-001")
|
11 |
+
messages = [
|
12 |
+
ChatMessage(
|
13 |
+
role="system", content="You are a pirate with a colorful personality"
|
14 |
+
),
|
15 |
+
ChatMessage(role="user", content="What is your name"),
|
16 |
+
]
|
17 |
+
resp = llm.stream_chat(messages)
|
18 |
+
|
19 |
+
for r in resp:
|
20 |
+
print(r.delta, end="")
|
21 |
+
|
22 |
|
23 |
if True:
|
24 |
# Load modules from src
|
src/llm_factory.py
CHANGED
@@ -12,9 +12,18 @@ from llama_index.llms.openrouter import OpenRouter
|
|
12 |
|
13 |
__all__ = ["get_llm", "get_available_llms"]
|
14 |
|
15 |
-
#
|
|
|
|
|
16 |
_dotenv_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".env"))
|
17 |
-
_dotenv_dict = dotenv_values(dotenv_path=_dotenv_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
_config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "llm_config.json"))
|
19 |
|
20 |
|
|
|
12 |
|
13 |
__all__ = ["get_llm", "get_available_llms"]
|
14 |
|
15 |
+
# Load .env values and merge with system environment variables.
|
16 |
+
# This one-liner makes sure any secret injected by Hugging Face, like OPENROUTER_API_KEY
|
17 |
+
# overrides what’s in your .env file.
|
18 |
_dotenv_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".env"))
|
19 |
+
_dotenv_dict = {**dotenv_values(dotenv_path=_dotenv_path), **os.environ}
|
20 |
+
print(f"Number of environment items: {len(_dotenv_dict)}")
|
21 |
+
|
22 |
+
if "OPENROUTER_API_KEY" in _dotenv_dict:
|
23 |
+
print("OPENROUTER_API_KEY is present")
|
24 |
+
else:
|
25 |
+
print("OPENROUTER_API_KEY is not present")
|
26 |
+
|
27 |
_config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "llm_config.json"))
|
28 |
|
29 |
|