File size: 3,314 Bytes
20d87f0 | 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 | import json
import requests
import os
from openai import OpenAI
from prompts import assistant_instructions
OPENAI_API_KEY = 'sk-proj-W6ywG0a4AK2D0QwR2gb6T3BlbkFJyEvw8S8w3OPZqhQH8BQj'
AIRTABLE_API_KEY = 'patLRH1ZoylUnzvTg.7eb42b0cee24ad273f91d302a9b90b37233b90e644a1035152d75fdb5cc22bc6'
# Init OpenAI Client
client = OpenAI(api_key=OPENAI_API_KEY)
# Add lead to Airtable
def create_lead(name, phone):
url = "https://api.airtable.com/v0/appM1yx0NobvowCAg/Accelerator%20Leads"
headers = {
"Authorization": AIRTABLE_API_KEY,
"Content-Type": "application/json"
}
data = {"records": [{"fields": {"Name": name, "Phone": phone}}]}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
print("Lead created successfully.")
return response.json()
else:
print(f"Failed to create lead: {response.text}")
# Create or load assistant
def create_assistant(client):
assistant_file_path = 'assistant.json'
# If there is an assistant.json file already, then load that assistant
if os.path.exists(assistant_file_path):
with open(assistant_file_path, 'r') as file:
assistant_data = json.load(file)
assistant_id = assistant_data['assistant_id']
print("Loaded existing assistant ID.")
else:
# If no assistant.json is present, create a new assistant using the below specifications
# To change the knowledge document, modify the file name below to match your document
# If you want to add multiple files, paste this function into ChatGPT and ask for it to add support for multiple files
file = client.files.create(file=open("knowledge.docx", "rb"),
purpose='assistants')
assistant = client.beta.assistants.create(
# Change prompting in prompts.py file
instructions=assistant_instructions,
model="gpt-4-1106-preview",
tools=[
{
"type": "retrieval" # This adds the knowledge base as a tool
},
{
"type": "function", # This adds the lead capture as a tool
"function": {
"name": "create_lead",
"description":
"Capture lead details and save to Airtable.",
"parameters": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Full name of the lead."
},
"phone": {
"type":
"string",
"description":
"Phone number of the lead including country code."
}
},
"required": ["name", "phone"]
}
}
}
],
file_ids=[file.id])
# Create a new assistant.json file to load on future runs
with open(assistant_file_path, 'w') as file:
json.dump({'assistant_id': assistant.id}, file)
print("Created a new assistant and saved the ID.")
assistant_id = assistant.id
return assistant_id
|