-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
90 lines (74 loc) · 2.56 KB
/
Copy pathapp.py
File metadata and controls
90 lines (74 loc) · 2.56 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
import os
import json
import openai
from flask import Flask, request, jsonify, render_template
from dotenv import load_dotenv
import requests
# Load environment variables
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
# Initialize the Flask app
app = Flask(__name__)
# Function to call CatAPI
def get_cat_images(limit, breed):
url = 'https://api.thecatapi.com/v1/images/search'
params = {'limit': limit, 'breed_id': breed}
headers = {
'x-api-key': os.getenv("CAT_API_KEY") # Add your Cat API key here
}
response = requests.get(url, params=params, headers=headers)
if response.status_code == 200:
return response.json()
else:
return []
function_descriptions = [
{
"name": "get_cat_images",
"description": "Get cat images by the breed",
"parameters": {
"type": "object",
"properties": {
"breed": {
"type": "string",
"description": "The breed of the cat, indexed by the first 4 letters, e.g. abys",
},
"limit": {
"type": "integer",
"description": "The number of cat images, e.g. 3",
},
},
},
}
]
@app.route('/')
def index():
return render_template('index.html')
@app.route('/chat', methods=['POST'])
def chat():
user_input = request.json.get('message')
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=[{"role": "user", "content": user_input}],
functions=function_descriptions,
function_call="auto",
)
output = completion.choices[0].message
response = "Please key in cat breed."
image_urls = None
if output.get("function_call"):
params = json.loads(output["function_call"]["arguments"])
limit = params.get("limit")
breed = params.get("breed")
cat_images = get_cat_images(limit, breed)
image_urls = [img['url'] for img in cat_images]
second_completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=[
{"role": "system", "content": "I do not want any URLs or links in the response. I just want one fun fact about the cat breed in the first line."},
{"role": "user", "content": user_input},
],
)
response = second_completion.choices[0].message.content
return jsonify({"message": response, "cat_images": image_urls})
if __name__ == '__main__':
app.run(debug=True)