Update app.py
Browse files
app.py
CHANGED
@@ -47,7 +47,7 @@ def read_json_from_s3():
|
|
47 |
|
48 |
return json_content
|
49 |
|
50 |
-
def get_titan_embedding(
|
51 |
"""
|
52 |
Retrieves a text embedding for a given document using the Amazon Titan Embedding model.
|
53 |
|
@@ -77,7 +77,7 @@ def get_titan_embedding(bedrock, doc_name, text, attempt=0, cutoff=10000):
|
|
77 |
})
|
78 |
|
79 |
# Invoke model
|
80 |
-
response =
|
81 |
body=body,
|
82 |
modelId=model_id,
|
83 |
accept=accept,
|
@@ -96,14 +96,14 @@ def get_titan_embedding(bedrock, doc_name, text, attempt=0, cutoff=10000):
|
|
96 |
|
97 |
delay = 2 ** (attempt + 1);
|
98 |
time.sleep(delay)
|
99 |
-
return get_titan_embedding(doc_name, text, attempt=attempt + 1)
|
100 |
|
101 |
elif error.response['Error']['Code'] == 'ValidationException':
|
102 |
# get chunks of text length 20000 characters
|
103 |
text_chunks = [text[i:i+cutoff] for i in range(0, len(text), cutoff)]
|
104 |
embeddings = []
|
105 |
for chunk in text_chunks:
|
106 |
-
embeddings.append(get_titan_embedding(
|
107 |
|
108 |
# return the average of the embeddinngs
|
109 |
return np.mean(embeddings, axis=0)
|
@@ -129,8 +129,6 @@ def ask_ds(message, history):
|
|
129 |
|
130 |
# RAG
|
131 |
question_embedding = get_titan_embedding(bedrock_client, 'question', question)
|
132 |
-
|
133 |
-
yield f'question: {question}\nmessage: {message}\nquestion embed: {question_embedding}'
|
134 |
|
135 |
similar_documents = []
|
136 |
for file, data in extractions.items():
|
|
|
47 |
|
48 |
return json_content
|
49 |
|
50 |
+
def get_titan_embedding(bedrock_client, doc_name, text, attempt=0, cutoff=10000):
|
51 |
"""
|
52 |
Retrieves a text embedding for a given document using the Amazon Titan Embedding model.
|
53 |
|
|
|
77 |
})
|
78 |
|
79 |
# Invoke model
|
80 |
+
response = bedrock_client.invoke_model(
|
81 |
body=body,
|
82 |
modelId=model_id,
|
83 |
accept=accept,
|
|
|
96 |
|
97 |
delay = 2 ** (attempt + 1);
|
98 |
time.sleep(delay)
|
99 |
+
return get_titan_embedding(bedrock_client, doc_name, text, attempt=attempt + 1)
|
100 |
|
101 |
elif error.response['Error']['Code'] == 'ValidationException':
|
102 |
# get chunks of text length 20000 characters
|
103 |
text_chunks = [text[i:i+cutoff] for i in range(0, len(text), cutoff)]
|
104 |
embeddings = []
|
105 |
for chunk in text_chunks:
|
106 |
+
embeddings.append(get_titan_embedding(bedrock_client, doc_name, chunk))
|
107 |
|
108 |
# return the average of the embeddinngs
|
109 |
return np.mean(embeddings, axis=0)
|
|
|
129 |
|
130 |
# RAG
|
131 |
question_embedding = get_titan_embedding(bedrock_client, 'question', question)
|
|
|
|
|
132 |
|
133 |
similar_documents = []
|
134 |
for file, data in extractions.items():
|