Spaces:
Sleeping
Sleeping
Create agent.py
Browse files
agent.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.prompts import PromptTemplate
|
| 2 |
+
from langchain.chains import LLMChain
|
| 3 |
+
from langchain_community.llms import OpenAI as LangOpenAI
|
| 4 |
+
from tools.search import web_search
|
| 5 |
+
|
| 6 |
+
def load_llm():
|
| 7 |
+
return LangOpenAI(temperature=0, model_name="gpt-4")
|
| 8 |
+
|
| 9 |
+
def build_prompt():
|
| 10 |
+
template = """You are a helpful research assistant. Answer the question using the context provided.
|
| 11 |
+
|
| 12 |
+
Context:
|
| 13 |
+
{context}
|
| 14 |
+
|
| 15 |
+
Question: {question}
|
| 16 |
+
Answer:"""
|
| 17 |
+
return PromptTemplate(template=template, input_variables=["question", "context"])
|
| 18 |
+
|
| 19 |
+
def answer_question(question, file_context=None, do_search=False):
|
| 20 |
+
context_parts = []
|
| 21 |
+
if file_context:
|
| 22 |
+
context_parts.append(file_context)
|
| 23 |
+
if do_search:
|
| 24 |
+
context_parts.append(web_search(question))
|
| 25 |
+
|
| 26 |
+
context = "\n\n".join(context_parts) if context_parts else "N/A"
|
| 27 |
+
|
| 28 |
+
llm = load_llm()
|
| 29 |
+
chain = LLMChain(llm=llm, prompt=build_prompt())
|
| 30 |
+
return chain.run({"question": question, "context": context}).strip()
|