Skip to content

Commit e6de51f

Browse files
authored
Merge pull request #266 from FullStackWithLawrence/next
incremental improvements to openai function calling for personal info
2 parents 0419d30 + ce870f0 commit e6de51f

File tree

8 files changed

+309
-28
lines changed

8 files changed

+309
-28
lines changed

README.md

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,39 @@
1717

1818
A [React](https://react.dev/) + [AWS Serverless](https://aws.amazon.com/serverless/) full stack implementation of the [30 example applications](https://platform.openai.com/examples) found in the official OpenAI API documentation. This repository is used as an instructional tool for the YouTube channel "[Full Stack With Lawrence](https://youtube.com/@FullStackWithLawrence)" as well as for University of British Columbia course, "[Artificial Intelligence Cloud Technology Implementation](https://extendedlearning.ubc.ca/courses/artificial-intelligence-cloud-technology-implementation/mg202)" taught by Lawrence McDaniel.
1919

20+
_New in version 0.10.x: A new chat app named "OpenAI Function Calling". See [lambda_openai_function](https://github.com/FullStackWithLawrence/aws-openai/tree/main/api/terraform/python/openai_api/lambda_openai_function) for examples including the fully implemented "[get_current_weather()](https://platform.openai.com/docs/guides/function-calling)" from The official OpenAI API documentation, and also a fun example of how get OpenAI to not only recognize you but also say flowery nice things about you!_
21+
2022
![Marv](https://cdn.lawrencemcdaniel.com/marv.gif)
2123

22-
**IMPORTANT DISCLAIMER: AWS' Lambda service has a hard 29-second timeout. OpenAI API calls often take longer than this, in which case the AWS API Gateway endpoint will return a 504 "Gateway timeout error" response to the React client. This happens frequently with apps created using chatgpt-4. Each of the 30 OpenAI API example applications are nonetheless implemented exactly as they are specified in the official documentation.**
24+
IMPORTANT DISCLAIMERS:
25+
26+
1. AWS' Lambda service has a hard 29-second timeout. OpenAI API calls often take longer than this, in which case the AWS API Gateway endpoint will return a 504 "Gateway timeout error" response to the React client. This happens frequently with apps created using chatgpt-4. Each of the 30 OpenAI API example applications are nonetheless implemented exactly as they are specified in the official documentation.
27+
28+
2. Distribution upload packages for AWS Lambda functions as well as AWS Lambda Layers are limited to 50mb (and 250mb unzipped). Often, this poses serious limitations for Layers, which are intended to store your PyPi / NPM package dependencies. Note that incidentally, these code samples are also pretty code scaffolding for alternative Docker-based deployment strategies using Elastic Container Service and/or Elastic Kubernetes Service.
2329

2430
Code composition as of Jan-2024:
2531

2632
```console
2733
-------------------------------------------------------------------------------
2834
Language files blank comment code
2935
-------------------------------------------------------------------------------
30-
Markdown 49 738 6 2257
31-
HCL 24 308 601 1990
32-
Python 17 420 437 1580
33-
YAML 19 111 101 1089
34-
JavaScript 39 111 126 1086
35-
JSX 6 42 44 793
36-
CSS 5 31 14 172
37-
make 1 26 29 109
36+
HCL 29 346 714 2324
37+
Markdown 51 751 6 2277
38+
Python 22 514 557 1955
39+
YAML 20 114 109 1204
40+
JavaScript 39 114 127 1085
41+
JSX 6 45 47 856
42+
CSS 5 32 14 180
43+
Text 6 13 0 116
44+
make 1 26 30 114
3845
INI 2 15 0 70
3946
HTML 2 1 0 65
40-
Text 3 5 0 64
4147
Jupyter Notebook 1 0 186 48
42-
Bourne Shell 4 13 42 36
48+
Bourne Shell 5 17 55 47
4349
TOML 1 1 0 23
4450
Dockerfile 1 4 4 5
4551
-------------------------------------------------------------------------------
46-
SUM: 174 1826 1590 9387
52+
SUM: 191 1,993 1,849 10,369
4753
-------------------------------------------------------------------------------
4854
```
4955

@@ -96,6 +102,11 @@ A REST API implementing each of the [30 example applications](https://platform.o
96102
- [NodeJS](https://nodejs.org/en/download): used with NPM for local ReactJS developer environment, and for configuring/testing Semantic Release.
97103
- [Docker Compose](https://docs.docker.com/compose/install/): used by an automated Terraform process to create the AWS Lambda Layer for OpenAI and LangChain.
98104

105+
Optional requirements:
106+
107+
- [Google Maps API key](https://developers.google.com/maps/documentation/geocoding/overview). This is used the OpenAI API Function Calling coding example, "[get_current_weather()](https://platform.openai.com/docs/guides/function-calling)".
108+
- [Pinecone API key](https://docs.pinecone.io/docs/quickstart). This is used for OpenAI API Embedding examples.
109+
99110
## Documentation
100111

101112
Detailed documentation for each endpoint is available here: [Documentation](./doc/examples/)

api/postman/OpenAI.postman_collection.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,17 +1422,17 @@
14221422
],
14231423
"body": {
14241424
"mode": "raw",
1425-
"raw": "{\n \"input_text\": \"Marv, I'd like to introduce you to all the nice YouTube viewers.\",\n \"chat_history\": []\n}",
1425+
"raw": "{\n \"input_text\": \"XMarv, I'd like to introduce you to all the nice YouTube viewers.\",\n \"chat_history\": []\n}",
14261426
"options": {
14271427
"raw": {
14281428
"language": "json"
14291429
}
14301430
}
14311431
},
14321432
"url": {
1433-
"raw": "{{base_url}}/examples/default-marv-sarcastic-chat",
1433+
"raw": "{{base_url}}/examples/openai-function-calling",
14341434
"host": ["{{base_url}}"],
1435-
"path": ["examples", "default-marv-sarcastic-chat"]
1435+
"path": ["examples", "openai-function-calling"]
14361436
}
14371437
},
14381438
"response": [
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# OpenAI Function Calling examples
2+
3+
## function_weather.py
4+
5+
Fully implements the "[get_current_weather()](https://platform.openai.com/docs/guides/function-calling)" from The official OpenAI API documentation. OpenAI's documentation provides scaffolding for this feature, but falls short of actually providing code that retrieves location-based current weather forecasts.
6+
7+
## function_refers_to.py
8+
9+
This module demonstrates an alternative implementation of prompt behavior modification involving both Function Calling, plus, dynamic modifications to the system prompt. This example relies on [lambda_config.yaml](./lambda_config.yaml) for personalization data.

api/terraform/python/openai_api/lambda_openai_function/function_refers_to.py

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66
import json
77

8+
import requests
89
import yaml
910
from openai_api.common.const import PYTHON_ROOT
1011
from openai_api.lambda_openai_function.natural_language_processing import does_refer_to
@@ -13,6 +14,13 @@
1314
with open(PYTHON_ROOT + "/openai_api/lambda_openai_function/lambda_config.yaml", "r", encoding="utf-8") as file:
1415
lambda_config = yaml.safe_load(file)
1516

17+
client_list_url = "https://api.lawrencemcdaniel.com/wp-json/wp/v2/posts?categories=46&tags=55&_embed&per_page=100"
18+
try:
19+
client_list_response = requests.get(client_list_url, timeout=60)
20+
# pylint: disable=broad-except
21+
except Exception as e:
22+
client_list_response = None
23+
1624

1725
def search_terms_are_in_messages(messages: list, search_terms: list = None, search_pairs: list = None) -> bool:
1826
"""
@@ -52,15 +60,46 @@ def customized_prompt(messages: list) -> list:
5260
return messages
5361

5462

55-
def get_additional_info(inquiry_type: str) -> str:
56-
"""Return a table of URLs and other info about the location"""
63+
def get_client_list() -> list:
64+
"""Return a list of clients"""
65+
if not client_list_response:
66+
return []
67+
68+
if client_list_response.status_code == 200:
69+
client_list = client_list_response.json()
70+
client_list = [
71+
client["title"]["rendered"] for client in client_list if "title" in client and "rendered" in client["title"]
72+
]
73+
return client_list
74+
return []
75+
76+
77+
def get_additional_info(inquiry_type: str = "biographical_info") -> str:
78+
"""Return select info from lambda_config.yaml"""
79+
inquiry_type = inquiry_type or "biographical_info"
80+
lambda_config["clients"] = get_client_list()
81+
82+
if inquiry_type == "client_list":
83+
return json.dumps(lambda_config["clients"])
84+
85+
if inquiry_type == "contact_info":
86+
return json.dumps(lambda_config["contact_information"])
87+
88+
if inquiry_type == "educational_info":
89+
return json.dumps(
90+
{
91+
"education": lambda_config["profile"]["education"],
92+
"certifications": lambda_config["profile"]["certifications"],
93+
}
94+
)
95+
5796
if inquiry_type == "biographical_info":
58-
return lambda_config["biographical_info"]
97+
return json.dumps(lambda_config)
5998

6099
if inquiry_type == "marketing_info":
61100
return json.dumps(lambda_config["marketing_info"])
62101

63-
return json.dumps({"error": "inquiry_type not recognized"})
102+
return json.dumps(lambda_config)
64103

65104

66105
def info_tool_factory():
@@ -72,11 +111,20 @@ def info_tool_factory():
72111
"type": "function",
73112
"function": {
74113
"name": "get_additional_info",
75-
"description": "Get additional information about Lawrence McDaniel, full stack web developer and host of YouTube channel FullStackwithLawrence.",
114+
"description": "Get additional information about Lawrence McDaniel, full stack web developer and host of YouTube channel FullStackwithLawrence. returns a personal bio, contact information, marketing information, client list, education background, professional certifications, etc.",
76115
"parameters": {
77116
"type": "object",
78117
"properties": {
79-
"inquiry_type": {"type": "string", "enum": ["biographical_info", "marketing_info"]},
118+
"inquiry_type": {
119+
"type": "string",
120+
"enum": [
121+
"biographical_info",
122+
"marketing_info",
123+
"contact_info",
124+
"educational_info",
125+
"client_list",
126+
],
127+
},
80128
},
81129
"required": ["inquiry_type"],
82130
},

api/terraform/python/openai_api/lambda_openai_function/lambda_config.yaml

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,19 @@ system_prompt: >
8080
Lawrence's Twitter is @FullStackWLarry. His email is [email protected].
8181
Lawrence's LinkedIn is https://www.linkedin.com/in/lawrencemcdaniel/.
8282
Lawrence's instructor profile at University of British Columbia is https://extendedlearning.ubc.ca/about-us/our-instructors/lawrence-mcdaniel
83-
83+
contact_information:
84+
- name: Lawrence P. McDaniel
85+
- title: Full Stack Developer
86+
- location: Mexico City
87+
- website: https://lawrencemcdaniel.com/contact
88+
- payments: https://clients.lawrencemcdaniel.com
89+
- linkedin: https://www.linkedin.com/in/lawrencemcdaniel/
90+
- whatsapp: +16178346172
91+
8492
marketing_info:
8593
name: Lawrence McDaniel
8694
title: Full Stack Developer
87-
location: Cambridge, MA
95+
location: Mexico City
8896
website: https://lawrencemcdaniel.com
8997
blog: https://blog.lawrencemcdaniel.com
9098
payments: https://clients.lawrencemcdaniel.com
@@ -93,10 +101,76 @@ marketing_info:
93101
twitter: https://twitter.com/FullStackWLarry
94102
youtube: https://www.youtube.com/@FullStackWithLawrence
95103
bio: https://extendedlearning.ubc.ca/about-us/our-instructors/lawrence-mcdaniel
96-
104+
flickr: https://www.flickr.com/photos/159844232@N02/
97105
biographical_info: >
98106
Lawrence McDaniel is a freelance full-stack web developer and online instructor at University of British Columbia. He has a BS in computer science and mathematics with minors in physics and English from University of North Texas. He was an early employee at three startups that were later acquired by Goldman Sachs, IBM and Deutsche Bank respectively, and has participated in a successful NASDAQ IPO. He has worked as a freelance technology consultant since 1999 and has advised dozens of startups as well as banks, hedge funds and real estate investment funds from around the world including Morgan Stanley, Fortress Investment Group, IBM Seterus, and others.
99107
He has expertise in data science, financial modeling and forecasting systems, analytics, machine learning, AI, big data, web, mobile, database technology, and Amazon Web Services (AWS) cloud infrastructure. He has 20+ years experience designing high performance, always-up transactional database systems using MySQL, MSSQL Server, Aurora and MongoDB. He also has years of expertise designing high availability, horizontally scalable cloud-based infrastructure environments. In fact, Lawrence is an ambassador for the AWS Activate Program in Mexico, helping startups and early-stage ventures get digital products and services to market using AWS. He advises companies on migration plans to AWS as well as works with early-stage ventures creating new highly scalable back-end environments and implementing continuous integration strategies.
100108
Lawrence is an angel investor, entrepreneur and company mentor with nearly 25 years of international experience with an emphasis on technology and US-Mexico trade. He has extensive startup, fund raising, business development and M&A experience with alternative investment and disruptive FinTech, edTech and IoT ventures. He is a mentor at Startup Mexico and Angel Ventures in Mexico City, and sits on the boards of edMex and M-Arca Foundation.
101109
But he’s no stranger to real work either! his family built the house where he grew up in rural northeast Texas, raised their own livestock, and farmed their own crops. He paid his way through college by working for six years during and after high school in various jobs as a printer, cattle hand, carpenter, roofer, sheet rocker, painter, glazier, welder, lumber yard worker, forklift mechanic, truck loader, landscaper and gardener, hot tub installer, janitor, grocery shelf stocker, carpet & commercial floor cleaner, dishwasher, waiter, and cook. During college he worked as a cafeteria food server, a tutor in the university math lab, a calculus paper grader and substitute lecturer for the math department and as a lab assistant for the physics department. Immediately after graduating college he helped launch one the first indoor rock climbing gyms, Exposure Indoor Rock Climbing gym in Carrollton, Texas, while simultaneously covering progress of NAFTA negotiations for a Dallas-based family office.
102110
Lawrence is fully English/Spanish bilingual and available to travel throughout all of North America.
111+
profile:
112+
education:
113+
- degree: Bachelors of Science
114+
majors:
115+
- Computer Science
116+
- Mathematics
117+
minors:
118+
- Physics
119+
- English
120+
university: University of North Texas
121+
year: 1992
122+
skills:
123+
- Full Stack Web Development
124+
- Python
125+
- ReactJS
126+
- OpenAI API
127+
- AWS
128+
- Kubernetes
129+
- Docker
130+
- MySQL
131+
- MongoDB
132+
certifications:
133+
- title: Data Science & Machine Learning - Making Data-driven Decisions
134+
issuer: Massachusetts Institute of Technology
135+
year: 2022
136+
link: https://verify.mygreatlearning.com/verify/OHMJTCTD
137+
- title: "LFS158x: Introduction to Kubernetes"
138+
issuer: Linux Foundation
139+
year: 2022
140+
link: https://courses.edx.org/certificates/79888712a08642a6b35c9c077d354bb6
141+
- title: "Front-End Web Development with React by Hong Kong University of Science and Technology"
142+
issuer: Coursera
143+
year: 2020
144+
link:
145+
- title: Accounting Analytics
146+
issuer: Wharton School of Business
147+
year: 2017
148+
link: https://www.coursera.org/account/accomplishments/certificate/EB4FBYKJ2956
149+
- title: Customer Analytics
150+
issuer: Wharton School of Business
151+
year: 2017
152+
link: https://www.coursera.org/account/accomplishments/certificate/4YXEDYZJXQUD
153+
- title: Operations Analytics
154+
issuer: Wharton School of Business
155+
year: 2017
156+
link: https://www.coursera.org/account/accomplishments/certificate/7NSB8MFBWCT8
157+
- title: People Analytics
158+
issuer: Wharton School of Business
159+
year: 2017
160+
link: https://www.coursera.org/account/accomplishments/certificate/KZN42KMZ4HHJ
161+
- title: Machine Learning
162+
issuer: Stanford University
163+
year: 2016
164+
link: https://www.coursera.org/account/accomplishments/certificate/2MVW9YRMFYC9
165+
projects:
166+
- title: Cookiecutter Open edX
167+
description: A community supported organization focused on providing reliable CI automation tools for Open edX™️ software deployed to Kubernetes with Tutor.
168+
links:
169+
- https://github.com/cookiecutter-openedx/
170+
year: 2022
171+
- title: Full Stack With Lawrence
172+
description: A YouTube channel dedicated to teaching full stack web development.
173+
links:
174+
- https://www.youtube.com/@FullStackWithLawrence
175+
- https://github.com/FullStackWithLawrence/
176+
year: 2021

0 commit comments

Comments
 (0)