igrisds commited on
Commit
b9b9709
1 Parent(s): 2b559c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -46
app.py CHANGED
@@ -67,55 +67,27 @@ def get_titan_embedding(bedrock_client, doc_name, text, attempt=0, cutoff=10000)
67
 
68
  retries = 5
69
 
70
- try:
71
- model_id = 'amazon.titan-embed-text-v1'
72
- accept = 'application/json'
73
- content_type = 'application/json'
74
-
75
- body = json.dumps({
76
- "inputText": text,
77
- })
78
-
79
- # Invoke model
80
- response = bedrock_client.invoke_model(
81
- body=body,
82
- modelId=model_id,
83
- accept=accept,
84
- contentType=content_type
85
- )
86
-
87
- # Print response
88
- response_body = json.loads(response['body'].read())
89
 
90
 
91
- # Handle a few common client exceptions
92
- except botocore.exceptions.ClientError as error:
93
- if error.response['Error']['Code'] == 'ThrottlingException':
94
- if attempt + 1 == retries:
95
- return None
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)
110
-
111
- else:
112
- yield f"Unhandled Exception when processing {doc_name}! : {error.response['Error']['Code']}"
113
- return None
114
 
115
- # Catch-all for any other exceptions
116
- except Exception as error:
117
- yield f"Unhandled Exception when processing {doc_name}: {type(error).__name__}"
118
- return None
119
 
120
  return response_body.get('embedding')
121
 
@@ -129,6 +101,8 @@ def ask_ds(message, history):
129
 
130
  # RAG
131
  question_embedding = get_titan_embedding(bedrock_client, 'question', question)
 
 
132
 
133
  similar_documents = []
134
  for file, data in extractions.items():
 
67
 
68
  retries = 5
69
 
70
+ model_id = 'amazon.titan-embed-text-v1'
71
+ accept = 'application/json'
72
+ content_type = 'application/json'
73
+
74
+ body = json.dumps({
75
+ "inputText": text,
76
+ })
77
+
78
+ # Invoke model
79
+ response = bedrock_client.invoke_model(
80
+ body=body,
81
+ modelId=model_id,
82
+ accept=accept,
83
+ contentType=content_type
84
+ )
85
+
86
+ # Print response
87
+ response_body = json.loads(response['body'].read())
 
88
 
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
 
 
 
 
91
 
92
  return response_body.get('embedding')
93
 
 
101
 
102
  # RAG
103
  question_embedding = get_titan_embedding(bedrock_client, 'question', question)
104
+
105
+ yield f"question embedding: {question_embedding}"
106
 
107
  similar_documents = []
108
  for file, data in extractions.items():