moritalous commited on
Commit
60f896a
·
1 Parent(s): 2787ed9

change boto3 to anthropic

Browse files
Files changed (2) hide show
  1. app.py +29 -41
  2. requirements.txt +1 -1
app.py CHANGED
@@ -5,7 +5,7 @@ import json
5
  import os
6
  from typing import List
7
 
8
- import boto3
9
  import requests
10
  import streamlit as st
11
  from googleapiclient.discovery import build
@@ -13,7 +13,9 @@ from googleapiclient.discovery import build
13
  st.title("Qiitaに聞いた!!")
14
 
15
  if "client" not in st.session_state:
16
- st.session_state.client = boto3.client("bedrock-runtime")
 
 
17
  client = st.session_state.client
18
 
19
 
@@ -28,33 +30,25 @@ def generate_search_queries(question: str) -> List[str]:
28
  Format: {"queries": ["query_1", "query_2", "query_3"]}
29
  """
30
 
31
- response = client.invoke_model(
32
- modelId="anthropic.claude-3-haiku-20240307-v1:0",
33
- body=json.dumps(
 
34
  {
35
- "anthropic_version": "bedrock-2023-05-31",
36
- "max_tokens": 1024,
37
- "system": "You are an expert at generating search queries for the Google search engine. Generate two search queries that are relevant to this question in Japanese. Output only valid JSON.",
38
- "messages": [
39
  {
40
- "role": "user",
41
- "content": [
42
- {
43
- "type": "text",
44
- "text": GENERATE_QUERIES.replace(
45
- "{{question}}", question
46
- ),
47
- }
48
- ],
49
- },
50
  ],
51
- "temperature": 0,
52
  }
53
- ),
 
 
54
  )
55
 
56
- result = json.loads(response.get("body").read())
57
- search_queries = result["content"][0]["text"]
58
  search_queries = json.loads(search_queries)
59
 
60
  return search_queries
@@ -133,30 +127,24 @@ def generate_answer(question: str, documents: dict):
133
  User's question: {question}
134
  """
135
 
136
- response = client.invoke_model(
137
- modelId="anthropic.claude-3-haiku-20240307-v1:0",
138
- body=json.dumps(
139
  {
140
- "anthropic_version": "bedrock-2023-05-31",
141
- "max_tokens": 1024,
142
- "messages": [
143
  {
144
- "role": "user",
145
- "content": [
146
- {
147
- "type": "text",
148
- "text": ANSWER_QUESTION,
149
- }
150
- ],
151
- },
152
  ],
153
- "temperature": 0.1,
154
  }
155
- ),
 
 
156
  )
157
 
158
- result = json.loads(response.get("body").read())
159
- return result["content"][0]["text"]
160
 
161
 
162
  # メイン関数
 
5
  import os
6
  from typing import List
7
 
8
+ from anthropic import Anthropic
9
  import requests
10
  import streamlit as st
11
  from googleapiclient.discovery import build
 
13
  st.title("Qiitaに聞いた!!")
14
 
15
  if "client" not in st.session_state:
16
+ st.session_state.client = Anthropic(
17
+ api_key=os.environ.get("ANTHROPIC_API_KEY"),
18
+ )
19
  client = st.session_state.client
20
 
21
 
 
30
  Format: {"queries": ["query_1", "query_2", "query_3"]}
31
  """
32
 
33
+ response = client.messages.create(
34
+ max_tokens=1024,
35
+ system="You are an expert at generating search queries for the Google search engine. Generate two search queries that are relevant to this question in Japanese. Output only valid JSON.",
36
+ messages=[
37
  {
38
+ "role": "user",
39
+ "content": [
 
 
40
  {
41
+ "type": "text",
42
+ "text": GENERATE_QUERIES.replace("{{question}}", question),
43
+ }
 
 
 
 
 
 
 
44
  ],
 
45
  }
46
+ ],
47
+ temperature=0,
48
+ model="claude-3-haiku-20240307",
49
  )
50
 
51
+ search_queries = response.content[0].text
 
52
  search_queries = json.loads(search_queries)
53
 
54
  return search_queries
 
127
  User's question: {question}
128
  """
129
 
130
+ response = client.messages.create(
131
+ max_tokens=1024,
132
+ messages=[
133
  {
134
+ "role": "user",
135
+ "content": [
 
136
  {
137
+ "type": "text",
138
+ "text": ANSWER_QUESTION,
139
+ }
 
 
 
 
 
140
  ],
 
141
  }
142
+ ],
143
+ temperature=0.1,
144
+ model="claude-3-haiku-20240307",
145
  )
146
 
147
+ return response.content[0].text
 
148
 
149
 
150
  # メイン関数
requirements.txt CHANGED
@@ -1,3 +1,3 @@
1
- boto3
2
  streamlit
3
  google-api-python-client
 
1
+ anthropic
2
  streamlit
3
  google-api-python-client