Spaces:
Build error
Build error
shigeru saito commited on
Commit ·
56dd884
1
Parent(s): 29c9545
エラー処理追加、GUIからcsvアップロードを削除、サンプルの修正
Browse files- app.py +19 -28
- sample.csv +6 -3
app.py
CHANGED
|
@@ -19,14 +19,8 @@ GOOGLE_API_KEY=os.environ["GOOGLE_API_KEY"]
|
|
| 19 |
|
| 20 |
def search_and_generate(question, prefix = "次の質問にできる限り答えてください。"):
|
| 21 |
# ツールの準備
|
| 22 |
-
search = GoogleSearchAPIWrapper()
|
| 23 |
-
|
| 24 |
-
name="Search",
|
| 25 |
-
func=search.run,
|
| 26 |
-
description="useful for when you need to answer questions about current events"
|
| 27 |
-
),
|
| 28 |
-
|
| 29 |
-
tools = load_tools(["google-search"], llm=ChatOpenAI())
|
| 30 |
|
| 31 |
# プロンプトテンプレートの準備
|
| 32 |
prefix = f"{prefix} 次のツールにアクセスできます:"
|
|
@@ -43,7 +37,7 @@ def search_and_generate(question, prefix = "次の質問にできる限り答え
|
|
| 43 |
)
|
| 44 |
|
| 45 |
# エージェントの準備
|
| 46 |
-
llm = ChatOpenAI(model_name="gpt-
|
| 47 |
llm_chain = LLMChain(llm=llm, prompt=prompt)
|
| 48 |
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools)
|
| 49 |
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)
|
|
@@ -52,7 +46,13 @@ def search_and_generate(question, prefix = "次の質問にできる限り答え
|
|
| 52 |
result = agent_executor.run(question)
|
| 53 |
except OutputParserException as e:
|
| 54 |
result = '例外が発生しました: OutputParserException ' + str(e.args[0])
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
return result
|
| 57 |
|
| 58 |
def search_and_generate_csv(csv_file):
|
|
@@ -87,21 +87,12 @@ def search_and_generate_csv(csv_file):
|
|
| 87 |
|
| 88 |
return result_csv
|
| 89 |
|
| 90 |
-
def process_input(str_question, file_csv):
|
| 91 |
# file_csvが空でない場合はCSVファイルとして扱う
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
return search_and_generate(str_question)
|
| 97 |
-
except HttpError as e:
|
| 98 |
-
print("An HTTP error %d occurred:\n%s" % (e.resp.status, e.content))
|
| 99 |
-
# reasonがrateLimitExceededの場合
|
| 100 |
-
if "rateLimitExceeded" in e.content.decode():
|
| 101 |
-
result = "検索APIリクエストの上限に達しました。"
|
| 102 |
-
else:
|
| 103 |
-
result = "検索API実行時に通信エラーが発生しました。"
|
| 104 |
-
return result
|
| 105 |
|
| 106 |
def main():
|
| 107 |
# パラメータに --csv がある場合、かつ、その次の引数がファイル名の場合
|
|
@@ -120,12 +111,12 @@ def main():
|
|
| 120 |
print(result)
|
| 121 |
else:
|
| 122 |
gr.Interface(fn=process_input,
|
| 123 |
-
inputs=[gr.Text()
|
| 124 |
outputs="text",
|
| 125 |
examples=[
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
],
|
| 130 |
).launch()
|
| 131 |
|
|
|
|
| 19 |
|
| 20 |
def search_and_generate(question, prefix = "次の質問にできる限り答えてください。"):
|
| 21 |
# ツールの準備
|
| 22 |
+
search = GoogleSearchAPIWrapper(k=1)
|
| 23 |
+
tools = load_tools(["google-search","requests"], llm=ChatOpenAI())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# プロンプトテンプレートの準備
|
| 26 |
prefix = f"{prefix} 次のツールにアクセスできます:"
|
|
|
|
| 37 |
)
|
| 38 |
|
| 39 |
# エージェントの準備
|
| 40 |
+
llm = ChatOpenAI(model_name="gpt-4")
|
| 41 |
llm_chain = LLMChain(llm=llm, prompt=prompt)
|
| 42 |
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools)
|
| 43 |
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)
|
|
|
|
| 46 |
result = agent_executor.run(question)
|
| 47 |
except OutputParserException as e:
|
| 48 |
result = '例外が発生しました: OutputParserException ' + str(e.args[0])
|
| 49 |
+
except HttpError as e:
|
| 50 |
+
print("An HTTP error %d occurred:\n%s" % (e.resp.status, e.content))
|
| 51 |
+
# reasonがrateLimitExceededの場合
|
| 52 |
+
if "rateLimitExceeded" in e.content.decode():
|
| 53 |
+
result = "検索APIリクエストの上限に達しました。"
|
| 54 |
+
else:
|
| 55 |
+
result = "検索API実行時に通信エラーが発生しました。"
|
| 56 |
return result
|
| 57 |
|
| 58 |
def search_and_generate_csv(csv_file):
|
|
|
|
| 87 |
|
| 88 |
return result_csv
|
| 89 |
|
| 90 |
+
def process_input(str_question, file_csv=None):
|
| 91 |
# file_csvが空でない場合はCSVファイルとして扱う
|
| 92 |
+
if file_csv:
|
| 93 |
+
return search_and_generate_csv(file_csv.name)
|
| 94 |
+
elif str_question:
|
| 95 |
+
return search_and_generate(str_question)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
def main():
|
| 98 |
# パラメータに --csv がある場合、かつ、その次の引数がファイル名の場合
|
|
|
|
| 111 |
print(result)
|
| 112 |
else:
|
| 113 |
gr.Interface(fn=process_input,
|
| 114 |
+
inputs=[gr.Text()],
|
| 115 |
outputs="text",
|
| 116 |
examples=[
|
| 117 |
+
"ChatGPTの最新のアップデートは?",
|
| 118 |
+
"日下部民藝館に伝わる伝承は?",
|
| 119 |
+
"箱根に伝わる伝説は?",
|
| 120 |
],
|
| 121 |
).launch()
|
| 122 |
|
sample.csv
CHANGED
|
@@ -1,4 +1,7 @@
|
|
| 1 |
question, answer
|
| 2 |
-
葛飾区に伝わる伝承は?
|
| 3 |
-
品川区に伝わる伝承は?
|
| 4 |
-
箱根に伝わる伝承は?
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
question, answer
|
| 2 |
+
葛飾区に伝わる伝承は?
|
| 3 |
+
品川区に伝わる伝承は?
|
| 4 |
+
箱根に伝わる伝承は?
|
| 5 |
+
日下部民藝館に伝わる伝承は?
|
| 6 |
+
醍醐寺に伝わる伝承は?
|
| 7 |
+
佐渡に伝わる伝承は?
|