Spaces:
Sleeping
Sleeping
shigeru saito
commited on
Commit
·
f56a350
1
Parent(s):
cc4cb18
gpt-4対応
Browse files
app.py
CHANGED
@@ -3,8 +3,10 @@ import dotenv
|
|
3 |
import sys
|
4 |
import gradio as gr
|
5 |
|
6 |
-
from langchain
|
7 |
-
from langchain import
|
|
|
|
|
8 |
|
9 |
dotenv.load_dotenv()
|
10 |
|
@@ -12,13 +14,20 @@ OPENAI_API_KEY=os.environ["OPENAI_API_KEY"]
|
|
12 |
GOOGLE_CSE_ID=os.environ["GOOGLE_CSE_ID"]
|
13 |
GOOGLE_API_KEY=os.environ["GOOGLE_API_KEY"]
|
14 |
|
15 |
-
def
|
16 |
# ツールの準備
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
# プロンプトテンプレートの準備
|
20 |
-
prefix = ""
|
21 |
-
suffix = """
|
22 |
|
23 |
Question: {input}
|
24 |
{agent_scratchpad}"""
|
@@ -31,9 +40,9 @@ def search_ang_generate(question):
|
|
31 |
)
|
32 |
|
33 |
# エージェントの準備
|
34 |
-
|
|
|
35 |
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools)
|
36 |
-
# agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools)
|
37 |
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)
|
38 |
|
39 |
result = agent_executor.run(question)
|
@@ -43,10 +52,10 @@ def search_ang_generate(question):
|
|
43 |
def main():
|
44 |
if len(sys.argv) > 1:
|
45 |
question = sys.argv[1]
|
46 |
-
result =
|
47 |
print(result)
|
48 |
else:
|
49 |
-
gr.Interface(fn=
|
50 |
|
51 |
if __name__ == "__main__":
|
52 |
-
|
|
|
3 |
import sys
|
4 |
import gradio as gr
|
5 |
|
6 |
+
from langchain import LLMChain
|
7 |
+
from langchain.agents import ZeroShotAgent, AgentExecutor, load_tools, Tool
|
8 |
+
from langchain.chat_models import ChatOpenAI
|
9 |
+
from langchain.utilities import GoogleSearchAPIWrapper
|
10 |
|
11 |
dotenv.load_dotenv()
|
12 |
|
|
|
14 |
GOOGLE_CSE_ID=os.environ["GOOGLE_CSE_ID"]
|
15 |
GOOGLE_API_KEY=os.environ["GOOGLE_API_KEY"]
|
16 |
|
17 |
+
def search_and_generate(question, prefix = "次の質問にできる限り答えてください。"):
|
18 |
# ツールの準備
|
19 |
+
search = GoogleSearchAPIWrapper()
|
20 |
+
search_tool = Tool(
|
21 |
+
name="Search",
|
22 |
+
func=search.run,
|
23 |
+
description="useful for when you need to answer questions about current events"
|
24 |
+
),
|
25 |
+
|
26 |
+
tools = load_tools(["google-search"], llm=ChatOpenAI())
|
27 |
|
28 |
# プロンプトテンプレートの準備
|
29 |
+
prefix = f"{prefix} 次のツールにアクセスできます:"
|
30 |
+
suffix = """始めましょう。
|
31 |
|
32 |
Question: {input}
|
33 |
{agent_scratchpad}"""
|
|
|
40 |
)
|
41 |
|
42 |
# エージェントの準備
|
43 |
+
llm = ChatOpenAI(model_name="gpt-4")
|
44 |
+
llm_chain = LLMChain(llm=llm, prompt=prompt)
|
45 |
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools)
|
|
|
46 |
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)
|
47 |
|
48 |
result = agent_executor.run(question)
|
|
|
52 |
def main():
|
53 |
if len(sys.argv) > 1:
|
54 |
question = sys.argv[1]
|
55 |
+
result = search_and_generate(question)
|
56 |
print(result)
|
57 |
else:
|
58 |
+
gr.Interface(fn=search_and_generate, inputs="text", outputs="text").launch()
|
59 |
|
60 |
if __name__ == "__main__":
|
61 |
+
main()
|