shigeru saito commited on
Commit
83f45c7
·
1 Parent(s): e34e51b
Files changed (1) hide show
  1. app.py +16 -11
app.py CHANGED
@@ -30,9 +30,13 @@ def fetch_folklore(location):
30
  with open('folklore.csv', 'r') as f:
31
  reader = csv.DictReader(f)
32
  folklore_lookup = {row['location']: row['folklore'] for row in reader}
 
33
 
34
- # 指定された地点の伝承を返す。存在しない場合は不明を返す。
35
- return folklore_lookup.get(location, "その地域の伝承は不明です。")
 
 
 
36
 
37
  def serialize_agent_action(obj):
38
  if isinstance(obj, AgentAction):
@@ -69,13 +73,12 @@ def get_response_from_function_calling(query_text):
69
  function_definitions = [
70
  # 関数の定義を作成
71
  {
72
- "name": "fetchFolklore",
73
  "description": "伝承を調べる",
74
  "parameters": {
75
  "type": "object",
76
  "properties": {
77
  "location": {
78
- "type": "string",
79
  "description": "伝承を知りたい施設や地名。例: 箱根",
80
  },
81
  },
@@ -84,7 +87,7 @@ def get_response_from_function_calling(query_text):
84
  }
85
  ]
86
  messages = [HumanMessage(content=query_text)]
87
- language_model = ChatOpenAI(model_name='gpt-3.5-turbo-0613')
88
  # 言語モデルを使ってメッセージを予測
89
  message = language_model.predict_messages(
90
  messages, functions=function_definitions)
@@ -96,9 +99,11 @@ def get_response_from_function_calling(query_text):
96
 
97
  # JSON 文字列を辞書に変換
98
  arguments = json.loads(arguments)
 
 
99
 
100
  # 関数を実行してレスポンスを取得
101
- function_response = fetch_folklore(location=arguments.get("location"))
102
  # 関数メッセージを作成
103
  function_message = FunctionMessage(
104
  name=function_name, content=function_response)
@@ -106,9 +111,9 @@ def get_response_from_function_calling(query_text):
106
  messages.append(function_message)
107
  second_response = language_model.predict_messages(
108
  messages=messages, functions=function_definitions)
109
- content = "AIの回答: " + second_response.content
110
  else:
111
- content = "AIの回答: " + message.content
112
  return content
113
 
114
  # Function Call Agentからレスポンスを取得する関数
@@ -183,8 +188,8 @@ if __name__ == "__main__":
183
  fn=main,
184
  examples=[
185
  ["葛飾区の伝承を教えてください。"],
186
- ["千代田にはどんな伝統がありますか?"],
187
- ["江戸川区にはどんな伝説がありますか?"],
188
  ],
189
  inputs=gr.Textbox(
190
  lines=5, placeholder="質問を入力してください"),
@@ -193,7 +198,7 @@ if __name__ == "__main__":
193
  gr.Textbox(label="Function Callingのレスポンス"),
194
  gr.Textbox(label="Function Calling Agentのレスポンス")
195
  ],
196
- title="日本各地の伝承AI",
197
  description="最新のGPTモデルを使用し、LangChain, Function Calling, Function Calling + LangChain Agentの対話モデルのAIから回答を取得するシステムです。以下のインプット例をクリックすると入力欄に自動入力されます。",
198
  example_columns=3,
199
  example_callback=click_example
 
30
  with open('folklore.csv', 'r') as f:
31
  reader = csv.DictReader(f)
32
  folklore_lookup = {row['location']: row['folklore'] for row in reader}
33
+ type_lookup = {row['type']: row['folklore'] for row in reader}
34
 
35
+ # 指定された地点の伝承などを返す。存在しない場合は不明を返す。
36
+ folklore = folklore_lookup.get((location), f"その地域の伝承は不明です。")
37
+ type = type_lookup.get((location), f"その地域の伝承は不明です。")
38
+ print("type:", type)
39
+ return folklore
40
 
41
  def serialize_agent_action(obj):
42
  if isinstance(obj, AgentAction):
 
73
  function_definitions = [
74
  # 関数の定義を作成
75
  {
76
+ "name": "fetch_folklore",
77
  "description": "伝承を調べる",
78
  "parameters": {
79
  "type": "object",
80
  "properties": {
81
  "location": {
 
82
  "description": "伝承を知りたい施設や地名。例: 箱根",
83
  },
84
  },
 
87
  }
88
  ]
89
  messages = [HumanMessage(content=query_text)]
90
+ language_model = ChatOpenAI(model_name='gpt-4')
91
  # 言語モデルを使ってメッセージを予測
92
  message = language_model.predict_messages(
93
  messages, functions=function_definitions)
 
99
 
100
  # JSON 文字列を辞書に変換
101
  arguments = json.loads(arguments)
102
+ location=arguments.get("location")
103
+ # type=arguments.get("type")
104
 
105
  # 関数を実行してレスポンスを取得
106
+ function_response = fetch_folklore(location=location)
107
  # 関数メッセージを作成
108
  function_message = FunctionMessage(
109
  name=function_name, content=function_response)
 
111
  messages.append(function_message)
112
  second_response = language_model.predict_messages(
113
  messages=messages, functions=function_definitions)
114
+ content = second_response.content
115
  else:
116
+ content = message.content
117
  return content
118
 
119
  # Function Call Agentからレスポンスを取得する関数
 
188
  fn=main,
189
  examples=[
190
  ["葛飾区の伝承を教えてください。"],
191
+ ["千代田区にはどんな伝承がありますか?"],
192
+ ["江戸川区で有名な伝承?"],
193
  ],
194
  inputs=gr.Textbox(
195
  lines=5, placeholder="質問を入力してください"),
 
198
  gr.Textbox(label="Function Callingのレスポンス"),
199
  gr.Textbox(label="Function Calling Agentのレスポンス")
200
  ],
201
+ title="東京23区の伝承",
202
  description="最新のGPTモデルを使用し、LangChain, Function Calling, Function Calling + LangChain Agentの対話モデルのAIから回答を取得するシステムです。以下のインプット例をクリックすると入力欄に自動入力されます。",
203
  example_columns=3,
204
  example_callback=click_example