pluto90 commited on
Commit
5f12493
·
verified ·
1 Parent(s): f9c7f62

Update app/core/llm_engine.py

Browse files
Files changed (1) hide show
  1. app/core/llm_engine.py +3 -136
app/core/llm_engine.py CHANGED
@@ -1,77 +1,8 @@
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
@@ -81,72 +12,8 @@ genai.configure(api_key=GEMINI_API_KEY)
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
-
 
1
+ # llm_engine.py
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  import google.generativeai as genai
4
  from app.core.config import GEMINI_API_KEY
5
+
 
6
  from langchain_google_genai import ChatGoogleGenerativeAI
7
 
8
  # ✅ Configure Gemini client
 
12
  llm = ChatGoogleGenerativeAI(
13
  model="gemini-2.5-flash",
14
  google_api_key=GEMINI_API_KEY,
15
+ temperature=0.2,
16
  max_output_tokens=500,
17
  convert_system_message_to_human=True
18
  )
19