-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
99 lines (80 loc) · 4.09 KB
/
main.py
File metadata and controls
99 lines (80 loc) · 4.09 KB
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
from flask import Flask, jsonify, request, render_template
import cohere
from serper import search_products
app = Flask(__name__, template_folder='templates')
cohere_client = cohere.Client("OsmGsKINTIyoKZVdhWpo968xMpX3CR0v4lWgrbON")
#Connecting the cohereAPI
def chat_completion(prompt):
try:
response = cohere_client.generate(
model="command-xlarge-nightly",
prompt=prompt,
max_tokens=300,
temperature=0.7
)
return response.generations[0].text.strip()
except Exception as e:
return f"Error with Cohere API: {str(e)}"
#Endpoint that will handle the search queries
@app.route('/search', methods=['POST'])
def search():
data = request.json
query = data.get('query')
region = data.get('region', 'gb')
if not query:
return jsonify({"error": "Missing 'query' field"}), 400
product_details = search_products(query, region)
if "error" in product_details:
return jsonify({"error": product_details["error"]}), 500
if not product_details.get('shopping'):
return jsonify({"error": "No products found for the given query"}), 404
products = product_details.get('shopping', [])[:20]
products_text = "\n".join([
f"- {item['title']} at {item['price']} (Source: {item['source']})"
for item in products
])
prompt = (
f"The following products were retrieved based on the search query '{query}':\n\n"
f"{products_text}\n\n"
"Please analyze all the options listed above and determine the best choice for the user based on price, quality, and value for money. "
"Provide a clear explanation for your recommendation, highlighting why the selected option stands out compared to the others. "
"Considering all of the factors mentioned, which product would you recommend to the user and why? Making it clear and short is key"
"Your response should be clear, concise, and easy to understand."
"The response should be at the very most 100 words long."
"The should not have any markdown, Html tags or any other special characters related to formatting."
"The response should be in English."
)
recommendation = chat_completion(prompt)
return jsonify({"products": products, "recommendation": recommendation})
#Home route rendering the serach form and display results
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
query = request.form.get('query')
region = request.form.get('region', 'gb')
product_details = search_products(query, region)
if "error" in product_details:
return render_template('index.html', error=product_details["error"])
if not product_details.get('shopping'):
return render_template('index.html', error="No products found for the given query.")
products_text = "\n".join([
f"- {item['title']} at {item['price']} (Source: {item['source']})"
for item in product_details.get('shopping', [])[:10]
])
prompt = (
f"The following products were retrieved based on the search query '{query}':\n\n"
f"{products_text}\n\n"
"Please analyze all the options listed above and determine the best choice for the user based on price, quality, and value for money. "
"Provide a clear explanation for your recommendation, highlighting why the selected option stands out compared to the others. "
"Considering all of the factors mentioned, which product would you recommend to the user and why? Making it clear and short is key"
"Your response should be clear, concise, and easy to understand."
"The response should be at the very most 100 words long."
"The should not have any markdown, Html tags or any other special characters related to formatting."
"The response should be in English."
)
response = chat_completion(prompt)
return render_template('index.html', result=response)
return render_template('index.html')
#Starting the flask project in debug mode
if __name__ == "__main__":
app.run(debug=True)