Spaces:
Sleeping
Sleeping
shigeru saito
commited on
Commit
·
cc4cb18
0
Parent(s):
first commit
Browse files- .env.example +3 -0
- .gitignore +1 -0
- app.py +52 -0
- requirements.txt +5 -0
.env.example
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
OPENAI_API_KEY=<OpenAI_APIキー>
|
2 |
+
GOOGLE_CSE_ID=<Googleカスタム検索_検索エンジンID>
|
3 |
+
GOOGLE_API_KEY=<Googleカスタム検索_APIキー>
|
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.env
|
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import dotenv
|
3 |
+
import sys
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor, load_tools
|
7 |
+
from langchain import OpenAI, SerpAPIWrapper, LLMChain
|
8 |
+
|
9 |
+
dotenv.load_dotenv()
|
10 |
+
|
11 |
+
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 search_ang_generate(question):
|
16 |
+
# ツールの準備
|
17 |
+
tools = load_tools(["google-search"], llm=OpenAI())
|
18 |
+
|
19 |
+
# プロンプトテンプレートの準備
|
20 |
+
prefix = """次の質問にできる限り答えてください。次のツールにアクセスできます:"""
|
21 |
+
suffix = """始めましょう!
|
22 |
+
|
23 |
+
Question: {input}
|
24 |
+
{agent_scratchpad}"""
|
25 |
+
|
26 |
+
prompt = ZeroShotAgent.create_prompt(
|
27 |
+
tools,
|
28 |
+
prefix=prefix,
|
29 |
+
suffix=suffix,
|
30 |
+
input_variables=["input", "agent_scratchpad"]
|
31 |
+
)
|
32 |
+
|
33 |
+
# エージェントの準備
|
34 |
+
llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)
|
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)
|
40 |
+
|
41 |
+
return result
|
42 |
+
|
43 |
+
def main():
|
44 |
+
if len(sys.argv) > 1:
|
45 |
+
question = sys.argv[1]
|
46 |
+
result = search_ang_generate(question)
|
47 |
+
print(result)
|
48 |
+
else:
|
49 |
+
gr.Interface(fn=search_ang_generate, inputs="text", outputs="text").launch()
|
50 |
+
|
51 |
+
if __name__ == "__main__":
|
52 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
openai
|
3 |
+
python-dotenv
|
4 |
+
google-api-python-client
|
5 |
+
gradio
|