Hey man, thanks.
I’ll post my Flask endpoint code below to give you an idea of what’s happening behind the scenes, just for some context this is interacting with my React front end:
@app.route('/api/courses-chat', methods=['POST'])
def handle_chat():
#---------------------------------------------------------------------------------
# Set Open AI Key (Kims)
#---------------------------------------------------------------------------------
openai.api_key = ""
#---------------------------------------------------------------------------------
# Define function variable for Open AI
#---------------------------------------------------------------------------------
function = [
{
"name": "get_courses",
"description": "Get course recommendations based on users criteria",
"parameters": {
"type": "object",
"properties": {
"skill_level": {
"type": "string",
"description": "The users current guitar ability or skill level"
},
"techniques": {
"type": "string",
"description": "Guitar techniques the user would like to learn or focus on"
},
"artists": {
"type": "string",
"description": "Artists and guitarists that the user likes"
},
"tutor": {
"type": "string",
"description": "Licklibrary tutors that the user likes"
}
},
"required": ["skill_level", "techniques", "artists", "tutor"]
}
}
]
#---------------------------------------------------------------------------------
# Define system prompt
#---------------------------------------------------------------------------------
with open("systemPrompt.txt") as file:
system = file.read()
#---------------------------------------------------------------------------------
# Define User prompt
#---------------------------------------------------------------------------------
user = "I need help learning the guitar"
user2 = "I am a beginner guitarist. I like BB King and I would like to focus on guitar licks. I also really like Danny Gill"
#---------------------------------------------------------------------------------
# Get conversation history
#---------------------------------------------------------------------------------
conversationHistory = request.json.get('conversationHistory', [])
# Ensure every message in conversationHistory has both a 'role' and a 'content'
for message in conversationHistory:
if not ('role' in message and 'content' in message):
return jsonify({"response": "Invalid message format in conversation history."})
# If conversationHistory is empty or lacks the system message, initialize it accordingly
if not conversationHistory or conversationHistory[0]['role'] != 'system':
conversationHistory.insert(0, {'role': 'system', 'content': system})
if len(conversationHistory) == 1:
conversationHistory.append({"role": "assistant", "content": "Hey, how can I help you on your Guitar journey today?"})
print(conversationHistory)
#---------------------------------------------------------------------------------
# Define function to be called (get_courses)
#---------------------------------------------------------------------------------
import pandas as pd
def get_courses(skill_level, techniques, artists, tutor):
# Load the dataset
df = pd.read_excel('products.xlsx')
# Prepare the criteria
keywords = skill_level.split() + techniques.split() + artists.split() + tutor.split()
# Function to count keyword matches in a string
def count_matches(string):
string = str(string) # Convert non-string values to strings
return sum(1 for keyword in keywords if keyword.lower() in string.lower())
# Apply the function to each specified column and sum the results
df['match_count'] = (
df['description'].apply(count_matches) +
df['meta_description'].apply(count_matches) +
df['artists'].apply(count_matches) +
df['tutors'].apply(count_matches) +
df['tags'].apply(count_matches) +
df['genres'].apply(count_matches) +
df['skills'].apply(count_matches) +
df['techniques'].apply(count_matches)
)
# Sort the DataFrame based on match count and take the top 3
top_courses = df.sort_values('match_count', ascending=False).head(3)
# Create an array of objects with the specified properties
courses_array = top_courses[['id', 'name', 'slug', 'image', 'store_image', 'tutors']].to_dict(orient='records')
# Convert the top courses to JSON
top_courses_json = top_courses.to_json(orient='records')
# Create a dictionary to hold both values
result = {
"top_courses": json.loads(top_courses_json),
"courses_array": courses_array
}
# Convert the result dictionary to a JSON string and return
return json.dumps(result)
#---------------------------------------------------------------------------------
# First call to Open AI Model
#---------------------------------------------------------------------------------
response_one = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=conversationHistory,
functions=function,
function_call="auto"
)
#---------------------------------------------------------------------------------
# Get response and print data
#---------------------------------------------------------------------------------
output = response_one.choices[0].message
print(output)
#---------------------------------------------------------------------------------
# Second call to Open AI API - First we check if function_call property is present in the returned object
#---------------------------------------------------------------------------------
if "function_call" in output and output.function_call is not None:
# Get the criteria object
params = json.loads(output.function_call.arguments)
chosen_function = eval(output.function_call.name)
user_criteria = chosen_function(**params)
#print(f"Params: {params}")
# Call Open AI sending users criteria
response_two = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=[
{"role": "system", "content": system},
{"role": "function", "name": output.function_call.name, "content": user_criteria}
],
functions=function,
)
assistant = response_two['choices'][0]['message']['content']
# Parse the user_criteria to get the overview and courses_data
user_criteria_data = json.loads(user_criteria)
top_courses = user_criteria_data.get('top_courses', [])
courses_data = user_criteria_data.get('courses_array', [])
# Create a summary string based on the top_courses data
overview = "Here are the top 3 courses based on your preferences:\n"
for i, course in enumerate(top_courses, 1):
overview += f"{i}. {course['name']} by {course['tutors']}\n"
# Structure the response
response_data = {
"overview": overview,
"courses_data": courses_data
}
return jsonify(response_data)
else:
return jsonify({"response": response_one['choices'][0]['message']['content']})
Here is a small sample from my dataset:
id |
name |
slug |
product_code |
type |
active |
ultimate_collection |
only_show_in_course |
description |
meta_description |
image |
store_image |
alt_tag |
trailer |
preview_duration |
free_to_all |
unlisted |
noindex |
artists |
tutors |
tags |
genres |
skills |
techniques |
series |
1408 |
(I Can’t Get No) Satisfaction Guitar Backing Track |
i-cant-get-no-satisfaction-guitar-backing-track |
NULL |
backing-track |
1 |
NULL |
NULL |
This Rolling Stones easy guitar backing track lets you play that famous Keith Richards ‘Satisfaction‘ guitar riff with a full band. |
This Rolling Stones easy guitar backing track lets you play that famous Keith Richards ‘Satisfaction‘ guitar riff with a full band. |
|
|
(I Can’t Get No) Satisfaction |
NULL |
90 |
NULL |
NULL |
NULL |
Rolling Stones |
NULL |
NULL |
Rock |
Pop |
Suitable For All |
Easy |
7623 |
12 Bar Blues Progression |
12-bar-blues-progression |
RDR0413 |
song-lesson |
1 |
NULL |
1 |
NULL |
NULL |
|
|
12 Bar Blues Progression |
NULL |
180 |
NULL |
NULL |
NULL |
Joe Bonamassa |
Danny Gill |
Eric Clapton |
Eric Johnson |
Joe Bonamassa |
Blues |
Rock |
37152 |
‘80s Pop F#m Guitar Backing Track |
80s-pop-fm-guitar-backing-track |
RDR0003 |
backing-track |
1 |
NULL |
NULL |
F# minor/Aeolian (A major) scales are a good choice for soloing over this dreamy guitar backing track, but more memorable melodies can be created with F#m pentatonic. |
F# minor/aeolian ballad guitar backing track |
|
|
LTP Your Own Rock Solos Minor Jamtrax |
NULL |
90 |
NULL |
NULL |
NULL |
Stuart Bull |
Stuart Bull |
NULL |
Pop |
80’s Rock |
NULL |
NULL |