Skip to content

Commit

Permalink
Create assistant, initialise functions, init. knowledge base.
Browse files Browse the repository at this point in the history
  • Loading branch information
Grigin committed Mar 9, 2024
1 parent ecfbf05 commit d9c0a82
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env.twilio_environment
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ RDS_PORT=1433
RDS_DATABASE=LexIssueData

OPENAI_ASSISTANT_ID=asst_WXd3r3pRZkaC7Q2k7X8DtKh4
ELEVENLABS_API_KEY=
VOICE_ID=uKiGb1dv4ftgRlwtw84z
2 changes: 2 additions & 0 deletions functions/types/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export interface TwilioEnvironmentVariables extends EnvironmentVariables {
ACCOUNT_SID: string;
AUTH_TOKEN: string;
OPENAI_ASSISTANT_ID: string;
ELEVENLABS_API_KEY: string;
VOICE_ID: string;
}

export interface SQLParam {
Expand Down
167 changes: 167 additions & 0 deletions scripts/assistant_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import json
import datetime
import numpy as np
import pandas as pd
from openai import OpenAI
import os
from typing import Sequence, Dict, Optional

MODEL = "gpt-4-1106-preview"
PROMPT_PATH = 'data/prompt.txt'
KNOWLEDGE_BASE_PATH = 'data/knowledge_base/'

OPENAI_API_KEY = "INSERT_KEY_HERE"


def init_functions():
tools = [
{
"type": "function",
"function": {
"name": "save_user_name",
"description": "Save the user's first and last name for personalized interactions",
"parameters": {
"type": "object",
"properties": {
"firstName": {
"type": "string",
"description": "The user's first name"
},
"lastName": {
"type": "string",
"description": "The user's last name"
}
},
"required": [
"firstName",
"lastName"
]
}
}
},
{
"type": "function",
"function": {
"name": "save_user_address",
"description": "Save the user's address for future reference",
"parameters": {
"type": "object",
"properties": {
"street": {
"type": "string",
"description": "The user's street address"
},
"house_number": {
"type": "string",
"description": "The user's house number"
},
"post_code": {
"type": "string",
"description": "The user's postal code"
},
"address": {
"type": "string",
"description": "The user's full address (optional, if you still want to provide it as a single string)"
}
},
"required": [
"street",
"house_number",
"post_code"
]
}
}
},
{
"type": "function",
"function": {
"name": "determine_bag_type",
"description": "Determines which type of rubbish bags a user wants (recycling or general waste)",
"parameters": {
"type": "object",
"properties": {
"bag_type": {
"type": "string",
"description": "The type of rubbish bags the user wants. Should be either 'recycling' or 'general waste'."
}
},
"required": [
"bag_type"
]
}
}
},
{"type": "retrieval"},
]

return tools


def update_date_in_prompt_file(prompt_path):
# Get the current date
current_date = datetime.datetime.now().strftime('%d/%m/%Y')

with open(prompt_path, 'r') as file:
content = file.read()

updated_content = content.replace('{current_date}', current_date)

with open(prompt_path, 'w') as file:
file.write(updated_content)


def clean_up(prompt_path):
with open(prompt_path, 'r') as file:
content = file.read()

current_date = datetime.datetime.now().strftime('%d/%m/%Y')
updated_content = content.replace(f'{current_date}', '{current_date}')

with open(prompt_path, 'w') as file:
file.write(updated_content)


def load_knowledge_base(client, dir_path):
ids = []
for filename in os.listdir(dir_path):
file_path = os.path.join(dir_path, filename)
if os.path.isfile(file_path):
with open(file_path, "rb") as file:
uploaded_file = client.files.create(
file=file,
purpose='assistants'
)
ids.append(uploaded_file.id)
return ids


def create_assistant(client):
update_date_in_prompt_file(prompt_path=PROMPT_PATH)
with open(PROMPT_PATH, 'r', encoding='utf-8') as file:
prompt = file.read()

assistant = client.beta.assistants.create(
name=f"Hillingdon Lex v1.{datetime.datetime.now().strftime('%d.%m.%Y')}",
description=" You are a kind, smart, and empathetic assistant named Hillingdon Lex.",
instructions=prompt,
model=MODEL,
tools=init_functions(),
file_ids=load_knowledge_base(client, KNOWLEDGE_BASE_PATH),

)

return assistant


def main():
client = OpenAI(
api_key=OPENAI_API_KEY
)

assistant = create_assistant(client)
thread = client.beta.threads.create()



if __name__ == "__main__":
main()
21 changes: 21 additions & 0 deletions scripts/data/prompt.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Today is {current_date}. You are a kind, smart, and empathetic assistant named Hillingdon Lex.

Hillingdon is an assistant designed to help residents of Hillingdon deal with council queries. So far Hillingdon helps with rubbish collection queries, ordering rubbish bags and can answer questions about this topic. This marks your first interaction with a new user.

No specific onboarding scenario is set, but here's a general guideline:

**Step 1:** Briefly introduce yourself, emphasizing your capability to assist with call center tasks. Don't rush to the next step straight after and wait for initial user response.

**Step 2:** Engage the user in a friendly conversation to gather key information:
- Ask for the user's name.
- Inquire about their address.

Always be polite and concise, ensuring you save the obtained information efficiently. Be mindful that the output of your chat will be then turned to speech and used in a phone conversation with a user, so keep your answers short – summarise, where you can. Maximum of three to five sentences.

If the user does not wish to share additional details, respect their choice and proceed.


If the user provides their details. Repeat what you've received from them and ask to confirm. Once the user confirms their name and address, save this information using the functions `save_user_name` and `save_user_address`.


Now, let's proceed with the onboarding process!

0 comments on commit d9c0a82

Please sign in to comment.