5
5
from langchain import LLMMathChain , SerpAPIWrapper
6
6
from langchain .agents import AgentType , Tool , initialize_agent
7
7
from langchain .callbacks import get_openai_callback
8
+ from langchain .chains import LLMChain
8
9
from langchain .llms import OpenAI
9
-
10
+ from langchain . prompts import PromptTemplate
10
11
11
12
openai .api_key = os .getenv ('OPENAI_API_KEY' )
12
13
os .environ ['SERPAPI_API_KEY' ] = os .getenv ('SERPAPI_API_KEY' )
@@ -51,6 +52,20 @@ def create_doc_chat(self, docGPT) -> Tool:
51
52
)
52
53
return tool
53
54
55
+ def create_llm_chain (self ) -> Tool :
56
+ """Add a llm tool"""
57
+ prompt = PromptTemplate (
58
+ input_variables = ['query' ],
59
+ template = '{query}'
60
+ )
61
+ llm_chain = LLMChain (llm = self .llm , prompt = prompt )
62
+
63
+ tool = Tool (
64
+ name = 'LLM' ,
65
+ func = llm_chain .run ,
66
+ description = 'useful for general purpose queries and logic'
67
+ )
68
+ return tool
54
69
def initialize (self , tools ):
55
70
for tool in tools :
56
71
if isinstance (tool , Tool ):
@@ -66,6 +81,9 @@ def initialize(self, tools):
66
81
def query (self , query : str ) -> Optional [str ]:
67
82
response = None
68
83
with get_openai_callback () as callback :
84
+ # TODO: The true result will hide in 'Observation'
85
+ # https://github.com/hwchase17/langchain/issues/4916
86
+ # https://python.langchain.com/docs/modules/agents/how_to/intermediate_steps
69
87
response = self .agent_ .run (query )
70
88
print (callback )
71
89
return response
0 commit comments