Download functions.py from Dixith/Gundu_bot: direct link, hf CLI and curl.
- Browser
- Download file 3.31 kB
-
https://huggingface.co/spaces/Dixith/Gundu_bot/resolve/main/functions.py
- Command line
-
hf download hf://spaces/Dixith/Gundu_bot/functions.py
-
curl -L -o functions.py https://huggingface.co/spaces/Dixith/Gundu_bot/resolve/main/functions.py
3.31 kB
| 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 | |