-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
128 lines (110 loc) · 4.51 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import streamlit as st
import json
import boto3
from googleapiclient.discovery import build
from botocore.exceptions import ClientError
import os
from dotenv import load_dotenv
# Load environment variables - try loading from .env file first, then fall back to st.secrets
load_dotenv()
def get_env_variable(key):
"""Get environment variable from .env file or Streamlit secrets"""
return os.getenv(key) or st.secrets.get(key)
# Function to fetch Google Search results
def google_search(query, api_key, cse_id, num_results=5):
try:
service = build("customsearch", "v1", developerKey=api_key)
res = service.cse().list(q=query, cx=cse_id, num=num_results).execute()
return [{'snippet': item['snippet'], 'link': item['link']} for item in res['items']]
except Exception as e:
st.error(f"Error performing Google search: {str(e)}")
return []
# Function to summarize text using AWS Bedrock
def summarize_text_with_aws(text, model_id):
try:
# Initialize the Bedrock Runtime client
client = boto3.client(
service_name='bedrock-runtime',
region_name='us-east-1',
aws_access_key_id=get_env_variable('AWS_ACCESS_KEY_ID'),
aws_secret_access_key=get_env_variable('AWS_SECRET_ACCESS_KEY')
)
# Prepare the request body for Claude
body = {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": f"Please summarize the following search results and provide a comprehensive answer:\n\n{text}"
}
],
"temperature": 0.7,
"top_p": 0.999,
}
# Invoke the model
response = client.invoke_model(
modelId=model_id,
contentType="application/json",
accept="application/json",
body=json.dumps(body)
)
# Parse the response
response_body = json.loads(response.get('body').read())
return response_body['content'][0]['text']
except ClientError as e:
if e.response['Error']['Code'] == 'AccessDeniedException':
st.error("AWS Access Denied. Please check your credentials.")
elif e.response['Error']['Code'] == 'ValidationException':
st.error("Invalid model ID or request format.")
else:
st.error(f"AWS Error: {str(e)}")
return None
except Exception as e:
st.error(f"Error generating summary: {str(e)}")
return None
# Streamlit App UI
def main():
st.set_page_config(page_title="Claudeplexity", page_icon=":mag:")
st.title("Claudeplexity: AI-Powered Research Assistant")
st.write("Get comprehensive answers with reliable sources")
# Add a nice search box
query = st.text_input(
"Ask anything...",
key="search_box",
help="Enter your question and press Enter"
)
if query:
# Load API credentials using the get_env_variable function
api_key = get_env_variable('GOOGLE_API_KEY')
cse_id = get_env_variable('GOOGLE_SEARCH_ENGINE_ID')
model_id = get_env_variable('BEDROCK_MODEL_ID')
if not all([api_key, cse_id]):
st.error("Missing API credentials")
return
with st.spinner('Searching and analyzing...'):
# Fetch Google Search results
search_results = google_search(query, api_key, cse_id)
if search_results:
# Prepare text for summarization
full_text = "\n\n".join([
f"Source ({result['link']}): {result['snippet']}"
for result in search_results
])
# Get summary from AWS Bedrock
summary = summarize_text_with_aws(full_text, model_id)
if summary:
# Display results in a nice format
st.subheader("Answer")
st.write(summary)
st.subheader("Sources")
for result in search_results:
with st.expander(f"Source: {result['link']}", expanded=False):
st.write(result['snippet'])
else:
st.warning("No search results found. Please try a different query.")
if __name__ == "__main__":
try:
main()
except Exception as e:
st.error(f"An error occurred: {str(e)}")