Spaces:
Running
Running
Update app/core/llm_engine.py
Browse files- app/core/llm_engine.py +152 -56
app/core/llm_engine.py
CHANGED
|
@@ -1,56 +1,152 @@
|
|
| 1 |
-
# llm_engine.py
|
| 2 |
-
|
| 3 |
-
import google.generativeai as genai
|
| 4 |
-
from app.core.config import GEMINI_API_KEY
|
| 5 |
-
from langchain_core.prompts import PromptTemplate
|
| 6 |
-
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 8 |
-
|
| 9 |
-
# β
Configure Gemini client
|
| 10 |
-
genai.configure(api_key=GEMINI_API_KEY)
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
def ask_gemini(context: str, question: str) -> str:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# # llm_engine.py
|
| 2 |
+
|
| 3 |
+
# import google.generativeai as genai
|
| 4 |
+
# from app.core.config import GEMINI_API_KEY
|
| 5 |
+
# from langchain_core.prompts import PromptTemplate
|
| 6 |
+
# from langchain_core.output_parsers import StrOutputParser
|
| 7 |
+
# from langchain_google_genai import ChatGoogleGenerativeAI
|
| 8 |
+
|
| 9 |
+
# # β
Configure Gemini client
|
| 10 |
+
# genai.configure(api_key=GEMINI_API_KEY)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# def ask_gemini(context: str, question: str) -> str:
|
| 14 |
+
# """
|
| 15 |
+
# Ask Gemini a question based on document context using LangChain for better formatting and control.
|
| 16 |
+
# """
|
| 17 |
+
|
| 18 |
+
# try:
|
| 19 |
+
# # β
Initialize Gemini LLM via LangChain
|
| 20 |
+
# llm = ChatGoogleGenerativeAI(
|
| 21 |
+
# model="gemini-2.5-flash",
|
| 22 |
+
# google_api_key=GEMINI_API_KEY,
|
| 23 |
+
# temperature=0.4,
|
| 24 |
+
# max_output_tokens=2048,
|
| 25 |
+
# convert_system_message_to_human=True
|
| 26 |
+
# )
|
| 27 |
+
|
| 28 |
+
# # β
Define a structured, formatting-rich prompt
|
| 29 |
+
# prompt = PromptTemplate(
|
| 30 |
+
# input_variables=["context", "question"],
|
| 31 |
+
# template=(
|
| 32 |
+
# "You are an intelligent document assistant.\n"
|
| 33 |
+
# "Answer the user's question strictly using the provided context.\n"
|
| 34 |
+
# "Respond in **clean Markdown formatting** with:\n"
|
| 35 |
+
# "- Headings (##)\n"
|
| 36 |
+
# "- Bullet points and numbered lists\n"
|
| 37 |
+
# "- **Bold keywords**\n"
|
| 38 |
+
# "- Tables (if useful)\n"
|
| 39 |
+
# "- Code blocks when necessary\n"
|
| 40 |
+
# "- Proper spacing and paragraphs for readability\n\n"
|
| 41 |
+
# "### π Document Context:\n{context}\n\n"
|
| 42 |
+
# "### π¬ User Question:\n{question}\n\n"
|
| 43 |
+
# "### π§ Answer:"
|
| 44 |
+
# )
|
| 45 |
+
# )
|
| 46 |
+
|
| 47 |
+
# # β
Combine the prompt, model, and parser (modern LCEL chain)
|
| 48 |
+
# chain = prompt | llm | StrOutputParser()
|
| 49 |
+
|
| 50 |
+
# # β
Run the chain
|
| 51 |
+
# response = chain.invoke({"context": context, "question": question})
|
| 52 |
+
|
| 53 |
+
# return response.strip() if response else "β οΈ No response from Gemini."
|
| 54 |
+
|
| 55 |
+
# except Exception as e:
|
| 56 |
+
# return f"β οΈ Gemini (LangChain) error: {str(e)}"
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# # llm_engine.py
|
| 70 |
+
|
| 71 |
+
import google.generativeai as genai
|
| 72 |
+
from app.core.config import GEMINI_API_KEY
|
| 73 |
+
from langchain_core.prompts import PromptTemplate
|
| 74 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 75 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 76 |
+
|
| 77 |
+
# β
Configure Gemini client
|
| 78 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
llm = ChatGoogleGenerativeAI(
|
| 82 |
+
model="gemini-2.5-flash",
|
| 83 |
+
google_api_key=GEMINI_API_KEY,
|
| 84 |
+
temperature=0.4,
|
| 85 |
+
max_output_tokens=500,
|
| 86 |
+
convert_system_message_to_human=True
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
# def ask_gemini(context: str, question: str) -> str:
|
| 91 |
+
# """
|
| 92 |
+
# Ask Gemini a question based on document context using LangChain for better formatting and control.
|
| 93 |
+
# """
|
| 94 |
+
|
| 95 |
+
# try:
|
| 96 |
+
# # β
Initialize Gemini LLM via LangChain
|
| 97 |
+
# # llm = ChatGoogleGenerativeAI(
|
| 98 |
+
# # model="gemini-2.5-flash",
|
| 99 |
+
# # google_api_key=GEMINI_API_KEY,
|
| 100 |
+
# # temperature=0.4,
|
| 101 |
+
# # max_output_tokens=2048,
|
| 102 |
+
# # convert_system_message_to_human=True
|
| 103 |
+
# # )
|
| 104 |
+
|
| 105 |
+
# # β
Define a structured, formatting-rich prompt
|
| 106 |
+
# prompt = PromptTemplate(
|
| 107 |
+
# input_variables=["context", "question"],
|
| 108 |
+
# template=(
|
| 109 |
+
# "You are an intelligent document assistant.\n"
|
| 110 |
+
# "Answer the user's question strictly using the provided context.\n"
|
| 111 |
+
# "Respond in **clean Markdown formatting** with:\n"
|
| 112 |
+
# "- Headings (##)\n"
|
| 113 |
+
# "- Bullet points and numbered lists\n"
|
| 114 |
+
# "- **Bold keywords**\n"
|
| 115 |
+
# "- Tables (if useful)\n"
|
| 116 |
+
# "- Code blocks when necessary\n"
|
| 117 |
+
# "- Proper spacing and paragraphs for readability\n\n"
|
| 118 |
+
# "### π Document Context:\n{context}\n\n"
|
| 119 |
+
# "### π¬ User Question:\n{question}\n\n"
|
| 120 |
+
# "### π§ Answer:"
|
| 121 |
+
# )
|
| 122 |
+
# )
|
| 123 |
+
|
| 124 |
+
# # β
Combine the prompt, model, and parser (modern LCEL chain)
|
| 125 |
+
# chain = prompt | llm | StrOutputParser()
|
| 126 |
+
|
| 127 |
+
# # β
Run the chain
|
| 128 |
+
# response = chain.invoke({"context": context, "question": question})
|
| 129 |
+
|
| 130 |
+
# return response.strip() if response else "β οΈ No response from Gemini."
|
| 131 |
+
|
| 132 |
+
# except Exception as e:
|
| 133 |
+
# return f"β οΈ Gemini (LangChain) error: {str(e)}"
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
# app/core/llm_engine.py
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
# def run_llm(prompt, inputs: dict):
|
| 143 |
+
# try:
|
| 144 |
+
# chain = prompt | llm | StrOutputParser()
|
| 145 |
+
# return chain.invoke(inputs)
|
| 146 |
+
# except Exception as e:
|
| 147 |
+
# return f"β οΈ LLM error: {str(e)}"
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
|