MaryamKarimi080 commited on
Commit
dfda80f
·
verified ·
1 Parent(s): eaac27f

Update scripts/router_chain.py

Browse files
Files changed (1) hide show
  1. scripts/router_chain.py +32 -33
scripts/router_chain.py CHANGED
@@ -1,33 +1,32 @@
1
- # scripts/router_simple.py
2
- from typing import Dict, Any
3
- from langchain.chat_models import ChatOpenAI
4
- from langchain.prompts import ChatPromptTemplate
5
- from scripts.rag_chat import build_general_qa_chain
6
-
7
- def build_router_chain(model_name=None):
8
- general_qa = build_general_qa_chain(model_name=model_name)
9
- llm = ChatOpenAI(model_name=model_name or "gpt-4o-mini", temperature=0.0)
10
-
11
- class Router:
12
- def invoke(self, input_dict: Dict[str, Any]):
13
- text = input_dict.get("input", "").lower()
14
- if "code" in text or "program" in text or "debug" in text:
15
- prompt = ChatPromptTemplate.from_template(
16
- "As a coding assistant, help with this Python question.\nQuestion: {input}\nAnswer:"
17
- )
18
- chain = prompt | llm
19
- return {"result": chain.invoke({"input": input_dict["input"]}).content}
20
- elif "summarize" in text or "summary" in text:
21
- prompt = ChatPromptTemplate.from_template(
22
- "Provide a concise summary about: {input}\nSummary:"
23
- )
24
- chain = prompt | llm
25
- return {"result": chain.invoke({"input": input_dict["input"]}).content}
26
- elif "calculate" in text or any(char.isdigit() for char in text):
27
- return {"result": "For calculations, please ask a specific calculation or provide more context."}
28
- else:
29
- # Use RAG chain
30
- result = general_qa({"query": input_dict["input"]})
31
- return result
32
-
33
- return Router()
 
1
+ from typing import Dict, Any
2
+ from langchain.chat_models import ChatOpenAI
3
+ from langchain.prompts import ChatPromptTemplate
4
+ from scripts.rag_chat import build_general_qa_chain
5
+
6
+ def build_router_chain(model_name=None):
7
+ general_qa = build_general_qa_chain(model_name=model_name)
8
+ llm = ChatOpenAI(model_name=model_name or "gpt-4o-mini", temperature=0.0)
9
+
10
+ class Router:
11
+ def invoke(self, input_dict: Dict[str, Any]):
12
+ text = input_dict.get("input", "").lower()
13
+ if "code" in text or "program" in text or "debug" in text:
14
+ prompt = ChatPromptTemplate.from_template(
15
+ "As a coding assistant, help with this Python question.\nQuestion: {input}\nAnswer:"
16
+ )
17
+ chain = prompt | llm
18
+ return {"result": chain.invoke({"input": input_dict["input"]}).content}
19
+ elif "summarize" in text or "summary" in text:
20
+ prompt = ChatPromptTemplate.from_template(
21
+ "Provide a concise summary about: {input}\nSummary:"
22
+ )
23
+ chain = prompt | llm
24
+ return {"result": chain.invoke({"input": input_dict["input"]}).content}
25
+ elif "calculate" in text or any(char.isdigit() for char in text):
26
+ return {"result": "For calculations, please ask a specific calculation or provide more context."}
27
+ else:
28
+ # Use RAG chain
29
+ result = general_qa({"query": input_dict["input"]})
30
+ return result
31
+
32
+ return Router()