Spaces:
Sleeping
Sleeping
Update app/core/llm_engine.py
Browse files- app/core/llm_engine.py +90 -24
app/core/llm_engine.py
CHANGED
|
@@ -1,41 +1,107 @@
|
|
| 1 |
# # llm_engine.py
|
| 2 |
|
| 3 |
-
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
from langchain_nvidia_ai_endpoints import ChatNVIDIA
|
| 7 |
-
import os
|
| 8 |
|
| 9 |
-
# ✅ Configure Gemini client
|
| 10 |
-
genai.configure(api_key=GEMINI_API_KEY)
|
| 11 |
|
| 12 |
-
# llm =
|
| 13 |
-
# model="
|
| 14 |
-
#
|
| 15 |
-
# temperature=0.
|
| 16 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# )
|
| 18 |
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
llm = ChatNVIDIA(
|
| 21 |
-
model=
|
| 22 |
-
api_key=
|
| 23 |
temperature=0.7,
|
| 24 |
-
max_tokens=1024
|
| 25 |
)
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
eval_llm = ChatNVIDIA(
|
| 28 |
-
model=
|
|
|
|
| 29 |
temperature=0.0,
|
| 30 |
-
max_tokens=200
|
| 31 |
)
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
# eval_llm = ChatGoogleGenerativeAI(
|
| 37 |
-
# model="gemini-2.0-flash",
|
| 38 |
-
# google_api_key=GEMINI_API_KEY,
|
| 39 |
-
# temperature=0.0,
|
| 40 |
-
# max_output_tokens=200,
|
| 41 |
-
# )
|
|
|
|
| 1 |
# # llm_engine.py
|
| 2 |
|
| 3 |
+
# from app.core.config import NVIDIA_API_KEY
|
| 4 |
+
# from langchain_nvidia_ai_endpoints import ChatNVIDIA
|
| 5 |
+
# import os
|
|
|
|
|
|
|
| 6 |
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# llm = ChatNVIDIA(
|
| 9 |
+
# model="meta/llama-3.1-70b-instruct",
|
| 10 |
+
# api_key=os.getenv("NVIDIA_API_KEY"),
|
| 11 |
+
# temperature=0.7,
|
| 12 |
+
# max_tokens=1024
|
| 13 |
+
# )
|
| 14 |
+
|
| 15 |
+
# eval_llm = ChatNVIDIA(
|
| 16 |
+
# # Faster for evaluation
|
| 17 |
+
# model="meta/llama-3.1-8b-instruct",
|
| 18 |
+
# temperature=0.0,
|
| 19 |
+
# max_tokens=200
|
| 20 |
# )
|
| 21 |
|
| 22 |
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# app/core/llm_engine.py
|
| 26 |
+
|
| 27 |
+
import os
|
| 28 |
+
|
| 29 |
+
from langchain_nvidia_ai_endpoints import ChatNVIDIA
|
| 30 |
+
|
| 31 |
+
# ============================================================
|
| 32 |
+
# Configuration
|
| 33 |
+
# ============================================================
|
| 34 |
+
|
| 35 |
+
NVIDIA_API_KEY = os.getenv("NVIDIA_API_KEY")
|
| 36 |
+
|
| 37 |
+
MAIN_MODEL = "meta/llama-3.1-70b-instruct"
|
| 38 |
+
EVAL_MODEL = "meta/llama-3.1-8b-instruct"
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# ============================================================
|
| 42 |
+
# Main LLM (Non-streaming)
|
| 43 |
+
# Used everywhere graph.invoke() is still called.
|
| 44 |
+
# ============================================================
|
| 45 |
+
|
| 46 |
llm = ChatNVIDIA(
|
| 47 |
+
model=MAIN_MODEL,
|
| 48 |
+
api_key=NVIDIA_API_KEY,
|
| 49 |
temperature=0.7,
|
| 50 |
+
max_tokens=1024,
|
| 51 |
)
|
| 52 |
|
| 53 |
+
# ============================================================
|
| 54 |
+
# Streaming LLM
|
| 55 |
+
# Used by /query-stream endpoint.
|
| 56 |
+
# Supports .astream()
|
| 57 |
+
# ============================================================
|
| 58 |
+
|
| 59 |
+
streaming_llm = ChatNVIDIA(
|
| 60 |
+
model=MAIN_MODEL,
|
| 61 |
+
api_key=NVIDIA_API_KEY,
|
| 62 |
+
temperature=0.7,
|
| 63 |
+
max_tokens=1024,
|
| 64 |
+
streaming=True,
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# ============================================================
|
| 68 |
+
# Evaluator LLM
|
| 69 |
+
# Faster + deterministic
|
| 70 |
+
# ============================================================
|
| 71 |
+
|
| 72 |
eval_llm = ChatNVIDIA(
|
| 73 |
+
model=EVAL_MODEL,
|
| 74 |
+
api_key=NVIDIA_API_KEY,
|
| 75 |
temperature=0.0,
|
| 76 |
+
max_tokens=200,
|
| 77 |
)
|
| 78 |
|
| 79 |
+
# ============================================================
|
| 80 |
+
# Helper getters
|
| 81 |
+
# (optional, but keeps imports clean)
|
| 82 |
+
# ============================================================
|
| 83 |
|
| 84 |
+
def get_llm():
|
| 85 |
+
"""
|
| 86 |
+
Standard synchronous LLM.
|
| 87 |
+
"""
|
| 88 |
+
return llm
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def get_streaming_llm():
|
| 92 |
+
"""
|
| 93 |
+
Streaming LLM.
|
| 94 |
+
Use with:
|
| 95 |
+
|
| 96 |
+
async for chunk in llm.astream(...):
|
| 97 |
+
...
|
| 98 |
+
"""
|
| 99 |
+
return streaming_llm
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def get_eval_llm():
|
| 103 |
+
"""
|
| 104 |
+
Evaluator model.
|
| 105 |
+
"""
|
| 106 |
+
return eval_llm
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|