├── .gitignore ├── 1_openai_basics_financial_data_extraction_tool ├── .idea │ ├── .gitignore │ ├── 1_openai_basics_financial_data_extraction_tool.iml │ ├── inspectionProfiles │ │ └── profiles_settings.xml │ ├── misc.xml │ ├── modules.xml │ └── vcs.xml ├── Extra │ └── openai_helper_do_not_use.py ├── README.md ├── main.py ├── openai_helper.py ├── requirements.txt ├── secret_key.py └── tool.jpg ├── 2_openai_functions ├── README.md ├── db │ ├── atliq_college_db.sql │ └── extra │ │ └── single_transaction_dump.sql ├── db_helper.py ├── image.jpg ├── main.py ├── openai_helper.py ├── secret_key.py └── test_functions.py └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | 3 | # Byte-compiled / optimized / DLL files 4 | __pycache__/ 5 | *.py[cod] 6 | *$py.class 7 | 8 | # C extensions 9 | *.so 10 | 11 | # Distribution / packaging 12 | .Python 13 | build/ 14 | develop-eggs/ 15 | dist/ 16 | downloads/ 17 | eggs/ 18 | .eggs/ 19 | lib/ 20 | lib64/ 21 | parts/ 22 | sdist/ 23 | var/ 24 | wheels/ 25 | share/python-wheels/ 26 | *.egg-info/ 27 | .installed.cfg 28 | *.egg 29 | MANIFEST 30 | 31 | # PyInstaller 32 | # Usually these files are written by a python script from a template 33 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 34 | *.manifest 35 | *.spec 36 | 37 | # Installer logs 38 | pip-log.txt 39 | pip-delete-this-directory.txt 40 | 41 | # Unit test / coverage reports 42 | htmlcov/ 43 | .tox/ 44 | .nox/ 45 | .coverage 46 | .coverage.* 47 | .cache 48 | nosetests.xml 49 | coverage.xml 50 | *.cover 51 | *.py,cover 52 | .hypothesis/ 53 | .pytest_cache/ 54 | cover/ 55 | 56 | # Translations 57 | *.mo 58 | *.pot 59 | 60 | # Django stuff: 61 | *.log 62 | local_settings.py 63 | db.sqlite3 64 | db.sqlite3-journal 65 | 66 | # Flask stuff: 67 | instance/ 68 | .webassets-cache 69 | 70 | # Scrapy stuff: 71 | .scrapy 72 | 73 | # Sphinx documentation 74 | docs/_build/ 75 | 76 | # PyBuilder 77 | .pybuilder/ 78 | target/ 79 | 80 | # Jupyter Notebook 81 | .ipynb_checkpoints 82 | 83 | # IPython 84 | profile_default/ 85 | ipython_config.py 86 | 87 | # pyenv 88 | # For a library or package, you might want to ignore these files since the code is 89 | # intended to run in multiple environments; otherwise, check them in: 90 | # .python-version 91 | 92 | # pipenv 93 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 94 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 95 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 96 | # install all needed dependencies. 97 | #Pipfile.lock 98 | 99 | # poetry 100 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 101 | # This is especially recommended for binary packages to ensure reproducibility, and is more 102 | # commonly ignored for libraries. 103 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 104 | #poetry.lock 105 | 106 | # pdm 107 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 108 | #pdm.lock 109 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 110 | # in version control. 111 | # https://pdm.fming.dev/#use-with-ide 112 | .pdm.toml 113 | 114 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 115 | __pypackages__/ 116 | 117 | # Celery stuff 118 | celerybeat-schedule 119 | celerybeat.pid 120 | 121 | # SageMath parsed files 122 | *.sage.py 123 | 124 | # Environments 125 | .env 126 | .venv 127 | env/ 128 | venv/ 129 | ENV/ 130 | env.bak/ 131 | venv.bak/ 132 | 133 | # Spyder project settings 134 | .spyderproject 135 | .spyproject 136 | 137 | # Rope project settings 138 | .ropeproject 139 | 140 | # mkdocs documentation 141 | /site 142 | 143 | # mypy 144 | .mypy_cache/ 145 | .dmypy.json 146 | dmypy.json 147 | 148 | # Pyre type checker 149 | .pyre/ 150 | 151 | # pytype static type analyzer 152 | .pytype/ 153 | 154 | # Cython debug symbols 155 | cython_debug/ 156 | 157 | # PyCharm 158 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 159 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 160 | # and can be added to the global gitignore or merged into this file. For a more nuclear 161 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 162 | #.idea/ 163 | -------------------------------------------------------------------------------- /1_openai_basics_financial_data_extraction_tool/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /1_openai_basics_financial_data_extraction_tool/.idea/1_openai_basics_financial_data_extraction_tool.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /1_openai_basics_financial_data_extraction_tool/.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /1_openai_basics_financial_data_extraction_tool/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /1_openai_basics_financial_data_extraction_tool/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /1_openai_basics_financial_data_extraction_tool/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /1_openai_basics_financial_data_extraction_tool/Extra/openai_helper_do_not_use.py: -------------------------------------------------------------------------------- 1 | import openai 2 | import json 3 | import pandas as pd 4 | import logging 5 | import os 6 | from secret_key import openapi_key 7 | 8 | openai.api_key = openapi_key 9 | 10 | def get_json_str_from_text(text): 11 | ''' 12 | :param text: sample text " xyz {"Company Name": "Tesla", "Stock Symbol": "TSLA" } .. abc" 13 | :return: "{"Company Name": "Tesla", "Stock Symbol": "TSLA" }" 14 | This function returns a valid json string from a long string containing that string plus some 15 | extra characters. OpenAI somethings returns these extra characters that we want to get rid of 16 | ''' 17 | import re 18 | pattern = r"\{([^{}]+)\}" 19 | match = re.search(pattern, text) 20 | if match: 21 | return match.group() 22 | 23 | return None 24 | 25 | def extract_financial_data(text): 26 | prompt = get_prompt_financial() + text 27 | response = openai.ChatCompletion.create( 28 | model="gpt-3.5-turbo", 29 | messages=[{"role": "user","content": prompt}] 30 | ) 31 | content = response.choices[0]['message']['content'] 32 | 33 | logging.info("\nOpenAI response:\n" + content + "\n-----\n") 34 | json_str = get_json_str_from_text(content) 35 | logging.info("\nJSON str:\n" + json_str + "\n-----\n") 36 | 37 | if json_str: 38 | try: 39 | data = json.loads(json_str) 40 | return pd.DataFrame(data.items(), columns=["Measure", "Value"]) 41 | except (json.JSONDecodeError, IndexError): 42 | # Handle the error when the argument is not a valid dictionary 43 | logging.error("Error: Invalid JSON string or empty list") 44 | 45 | return pd.DataFrame({ 46 | "Measure": ["Company Name", "Stock Symbol", "Revenue", "Net Income", "EPS"], 47 | "Value": ["", "", "", "", ""] 48 | }) 49 | 50 | 51 | def get_prompt_financial(): 52 | return '''Please retrieve company name, revenue, net income and earnings per share (a.k.a. EPS) 53 | from the following news article. If you can't find the information from this article 54 | then return "". Do not make things up. 55 | Then retrieve a stock symbol corresponding to that company. For this you can use 56 | your general knowledge (it doesn't have to be from this article). Always return your 57 | response as a valid JSON string. The format of that string should be this, 58 | { 59 | "Company Name": "Walmart", 60 | "Stock Symbol": "WMT", 61 | "Revenue": "12.34 million", 62 | "Net Income": "34.78 million", 63 | "EPS": "2.1 $" 64 | } 65 | News Article: 66 | ============ 67 | 68 | ''' 69 | 70 | def poem_on_samosa(): 71 | # legacy api call. They are suggesting to use ChatCompletion instead of Completion 72 | # response = openai.Completion.create( 73 | # model="text-davinci-003", 74 | # prompt="Can you write a poem on my love for samosa? Only 4 lines please.", 75 | # temperature=1, 76 | # max_tokens=50 77 | # ) 78 | # print(response.choices[0].text) 79 | 80 | response = openai.ChatCompletion.create( 81 | model="gpt-3.5-turbo", 82 | messages=[ 83 | { 84 | "role": "user", 85 | "content": "Can you write a poem on my love for samosa? Only 4 lines please." 86 | } 87 | ] 88 | ) 89 | 90 | print(response.choices[0]['message']['content']) 91 | 92 | if __name__ == "__main__": 93 | poem_on_samosa() 94 | 95 | -------------------------------------------------------------------------------- /1_openai_basics_financial_data_extraction_tool/README.md: -------------------------------------------------------------------------------- 1 | ### Financial Data Extraction Tool Using OpenAI API 2 | 3 | This tool is a streamlit based app that uses openai api to extract key financial measures such as company name, stock symbol, revenue, net income etc. from a news article. The news article is typically an article about company's finance reporting. 4 | 5 | ![Alt Text](./tool.jpg) 6 | 7 | 8 | ### Installation 9 | 10 | ```doctest 11 | pip install -r requirements.txt 12 | ``` 13 | You need to create an account on openai website. You will get inital 5$ free credits which is more than enough for this project. 14 | Once you get an api key from your account add it to secret_key.py 15 | -------------------------------------------------------------------------------- /1_openai_basics_financial_data_extraction_tool/main.py: -------------------------------------------------------------------------------- 1 | import streamlit as st 2 | import pandas as pd 3 | import openai_helper 4 | 5 | col1, col2 = st.columns([3,2]) 6 | 7 | financial_data_df = pd.DataFrame({ 8 | "Measure": ["Company Name", "Stock Symbol", "Revenue", "Net Income", "EPS"], 9 | "Value": ["", "", "", "", ""] 10 | }) 11 | 12 | with col1: 13 | st.title("Data Extraction Tool") 14 | news_article = st.text_area("Paste your financial news article here", height=300) 15 | if st.button("Extract"): 16 | financial_data_df = openai_helper.extract_financial_data(news_article) 17 | 18 | with col2: 19 | st.markdown("
" * 5, unsafe_allow_html=True) # Creates 5 lines of vertical space 20 | st.dataframe( 21 | financial_data_df, 22 | column_config={ 23 | "Measure": st.column_config.Column(width=150), 24 | "Value": st.column_config.Column(width=150) 25 | }, 26 | hide_index=True 27 | ) -------------------------------------------------------------------------------- /1_openai_basics_financial_data_extraction_tool/openai_helper.py: -------------------------------------------------------------------------------- 1 | import openai 2 | from secret_key import openai_key 3 | import json 4 | import pandas as pd 5 | 6 | openai.api_key = openai_key 7 | 8 | def extract_financial_data(text): 9 | prompt = get_prompt_financial() + text 10 | response = openai.ChatCompletion.create( 11 | model="gpt-3.5-turbo", 12 | messages=[{"role": "user","content": prompt}] 13 | ) 14 | content = response.choices[0]['message']['content'] 15 | 16 | try: 17 | data = json.loads(content) 18 | return pd.DataFrame(data.items(), columns=["Measure", "Value"]) 19 | 20 | except (json.JSONDecodeError, IndexError): 21 | pass 22 | 23 | return pd.DataFrame({ 24 | "Measure": ["Company Name", "Stock Symbol", "Revenue", "Net Income", "EPS"], 25 | "Value": ["", "", "", "", ""] 26 | }) 27 | 28 | 29 | 30 | def get_prompt_financial(): 31 | return '''Please retrieve company name, revenue, net income and earnings per share (a.k.a. EPS) 32 | from the following news article. If you can't find the information from this article 33 | then return "". Do not make things up. 34 | Then retrieve a stock symbol corresponding to that company. For this you can use 35 | your general knowledge (it doesn't have to be from this article). Always return your 36 | response as a valid JSON string. The format of that string should be this, 37 | { 38 | "Company Name": "Walmart", 39 | "Stock Symbol": "WMT", 40 | "Revenue": "12.34 million", 41 | "Net Income": "34.78 million", 42 | "EPS": "2.1 $" 43 | } 44 | News Article: 45 | ============ 46 | 47 | ''' 48 | 49 | # Press the green button in the gutter to run the script. 50 | if __name__ == '__main__': 51 | text = ''' 52 | Tesla's Earning news in text format: Tesla's earning this quarter blew all the estimates. They reported 4.5 billion $ profit against a revenue of 30 billion $. Their earnings per share was 2.3 $ 53 | ''' 54 | df = extract_financial_data(text) 55 | 56 | print(df.to_string()) -------------------------------------------------------------------------------- /1_openai_basics_financial_data_extraction_tool/requirements.txt: -------------------------------------------------------------------------------- 1 | openai 2 | streamlit 3 | pandas -------------------------------------------------------------------------------- /1_openai_basics_financial_data_extraction_tool/secret_key.py: -------------------------------------------------------------------------------- 1 | openai_key="add your key here" -------------------------------------------------------------------------------- /1_openai_basics_financial_data_extraction_tool/tool.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codebasics/openai-api-tutorials/07e21355480ed311bc0e6f8bd7ecf785fcb3a9fc/1_openai_basics_financial_data_extraction_tool/tool.jpg -------------------------------------------------------------------------------- /2_openai_functions/README.md: -------------------------------------------------------------------------------- 1 | # OpenAI Function Calling Tutorial For Beginners 2 | ### College management Q&A system using openai function calls 3 | 4 | ![](image.jpg) 5 | 6 | Build a system that can take input question related to a database of an imaginary college called AtliQ Commerce College. Here are some sample questions you can ask to this Q&A system. 7 | 8 | 9 | ### Sample questions 10 | 11 | 1. How much peter pandey have to pay in the first semester? 12 | 1. This should return "pending" fees 13 | 1. How much did peter pandey paid in the first semester? 14 | 1. This should return "paid" fees 15 | 1. What is the purpose of a balance sheet?. 16 | 1. This is not a question related to college's internal data. It is rather a general question that openai LLM can answer using its past training (similar to ChatGPT) 17 | 1. Average gpa in third semester? 18 | 1. This will return an average GPA of all the students in the third semester 19 | 20 | ## Tech Stack 21 | 22 | ```commandline 23 | Database: MySQL 24 | Backend: Python + OpenAI Function Calling 25 | Frontend: Streamlit 26 | ``` 27 | 28 | ## Set Up 29 | 30 | 1. Database: In db, directory you will see sql file. Just run that entire script in your MySQL workbench 31 | 2. Python code: You need to install following modules, 32 | ```commandline 33 | pip install openai 34 | pip install streamlit 35 | pip install mysql-connector-python 36 | ``` 37 | 3. OpenAI setup: You need to obtain a secret key from your openai account and put it in secret_key.py file 38 | 39 | ## Running streamlit app 40 | 41 | From your command line run this command, 42 | ```commandline 43 | streamlit run main.py 44 | ``` 45 | -------------------------------------------------------------------------------- /2_openai_functions/db/atliq_college_db.sql: -------------------------------------------------------------------------------- 1 | ##### Schema creation ##### 2 | 3 | CREATE TABLE marks ( 4 | student_name VARCHAR(100) NOT NULL, 5 | semester INT NOT NULL, 6 | gpa DECIMAL(3,2) NOT NULL, 7 | PRIMARY KEY (student_name, semester) 8 | ); 9 | 10 | CREATE TABLE fees ( 11 | student_name VARCHAR(100) NOT NULL, 12 | semester INT NOT NULL, 13 | total_fees DECIMAL(10,2) NOT NULL, 14 | paid DECIMAL(10,2) NOT NULL, 15 | pending DECIMAL(10,2) NOT NULL, 16 | PRIMARY KEY (student_name, semester) 17 | ); 18 | 19 | ##### Insert records ##### 20 | 21 | INSERT INTO marks (student_name, semester, gpa) 22 | VALUES 23 | ('Wanda Mishra', 1, 3.5), 24 | ('Lauki Lal', 1, 3.6), 25 | ('Peter Pandey', 1, 3.7), 26 | ('Thor Hathodawala', 1, 3.8), 27 | ('Virat Kohli', 1, 3.9), 28 | 29 | ('Sachin Tendulkar', 2, 3.2), 30 | ('Tom Cruise', 2, 3.3), 31 | ('Robert Downey', 2, 3.4), 32 | ('Steve Jobs', 2, 3.1), 33 | ('Bill Gates', 2, 3.0), 34 | 35 | ('Elon Musk', 3, 2.7), 36 | ('Tim Cook', 3, 2.6), 37 | ('Jeff Bezos', 3, 2.5), 38 | ('Mark Zuckerberg', 3, 2.8), 39 | ('Larry Page', 3, 2.9), 40 | 41 | ('Sergey Brin', 4, 3.1), 42 | ('Satya Nadella', 4, 3.2), 43 | ('Jack Ma', 4, 3.3), 44 | ('Sundar Pichai', 4, 3.4), 45 | ('Michael Dell', 4, 3.5); 46 | 47 | INSERT INTO fees (student_name, semester, total_fees, paid, pending) 48 | VALUES 49 | ('Wanda Mishra', 1, 5000, 2000, 3000), 50 | ('Lauki Lal', 1, 5000, 2500, 2500), 51 | ('Peter Pandey', 1, 5000, 3000, 2000), 52 | ('Thor Hathodawala', 1, 5000, 3500, 1500), 53 | ('Virat Kohli', 1, 5000, 4000, 1000), 54 | 55 | ('Sachin Tendulkar', 2, 6000, 2000, 4000), 56 | ('Tom Cruise', 2, 6000, 3000, 3000), 57 | ('Robert Downey', 2, 6000, 4000, 2000), 58 | ('Steve Jobs', 2, 6000, 5000, 1000), 59 | ('Bill Gates', 2, 6000, 6000, 0), 60 | 61 | ('Elon Musk', 3, 7000, 2000, 5000), 62 | ('Tim Cook', 3, 7000, 3000, 4000), 63 | ('Jeff Bezos', 3, 7000, 4000, 3000), 64 | ('Mark Zuckerberg', 3, 7000, 5000, 2000), 65 | ('Larry Page', 3, 7000, 6000, 1000), 66 | 67 | ('Sergey Brin', 4, 8000, 2000, 6000), 68 | ('Satya Nadella', 4, 8000, 3000, 5000), 69 | ('Jack Ma', 4, 8000, 4000, 4000), 70 | ('Sundar Pichai', 4, 8000, 5000, 3000), 71 | ('Michael Dell', 4, 8000, 6000, 2000); 72 | 73 | ### stored procedure #### 74 | 75 | DELIMITER $$ 76 | CREATE PROCEDURE get_marks (IN student_name VARCHAR(100), IN semester INT, IN operation VARCHAR(10)) 77 | BEGIN 78 | DECLARE result DECIMAL(3,2); 79 | 80 | IF student_name <> '' THEN 81 | SELECT gpa INTO result 82 | FROM marks 83 | WHERE LOWER(marks.student_name) = LOWER(student_name) AND marks.semester = semester; 84 | ELSEIF operation = 'max' THEN 85 | SELECT MAX(gpa) INTO result 86 | FROM marks 87 | WHERE marks.semester = semester; 88 | ELSEIF operation = 'min' THEN 89 | SELECT MIN(gpa) INTO result 90 | FROM marks 91 | WHERE marks.semester = semester; 92 | ELSEIF operation = 'avg' THEN 93 | SELECT AVG(gpa) INTO result 94 | FROM marks 95 | WHERE marks.semester = semester; 96 | END IF; 97 | 98 | -- Handling when no records are found 99 | IF result IS NULL THEN 100 | SET result = -1; 101 | END IF; 102 | 103 | SELECT result AS GPA; 104 | END$$ 105 | DELIMITER ; 106 | 107 | ### Stored procedure for fees ### 108 | DELIMITER $$ 109 | CREATE PROCEDURE get_fees (IN student_name VARCHAR(100), IN semester INT, IN fees_type VARCHAR(10)) 110 | BEGIN 111 | DECLARE result INT; 112 | 113 | IF student_name <> '' THEN 114 | IF fees_type = 'paid' THEN 115 | SELECT paid INTO result 116 | FROM fees 117 | WHERE LOWER(fees.student_name) = LOWER(student_name) AND fees.semester = semester; 118 | ELSEIF fees_type = 'pending' THEN 119 | SELECT pending INTO result 120 | FROM fees 121 | WHERE LOWER(fees.student_name) = LOWER(student_name) AND fees.semester = semester; 122 | ELSEIF fees_type = 'total' THEN 123 | SELECT total_fees INTO result 124 | FROM fees 125 | WHERE LOWER(fees.student_name) = LOWER(student_name) AND fees.semester = semester; 126 | END IF; 127 | ELSE 128 | IF fees_type = 'paid' THEN 129 | SELECT SUM(paid) INTO result 130 | FROM fees 131 | WHERE fees.semester = semester; 132 | ELSEIF fees_type = 'pending' THEN 133 | SELECT SUM(pending) INTO result 134 | FROM fees 135 | WHERE fees.semester = semester; 136 | ELSEIF fees_type = 'total' THEN 137 | SELECT SUM(total_fees) INTO result 138 | FROM fees 139 | WHERE fees.semester = semester; 140 | END IF; 141 | END IF; 142 | 143 | -- Handling when no records are found 144 | IF result IS NULL THEN 145 | SET result = -1; 146 | END IF; 147 | 148 | SELECT result AS Fees; 149 | END$$ 150 | DELIMITER ; 151 | 152 | -------------------------------------------------------------------------------- /2_openai_functions/db/extra/single_transaction_dump.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE IF NOT EXISTS `atliq_college_db` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */; 2 | USE `atliq_college_db`; 3 | -- MySQL dump 10.13 Distrib 8.0.31, for Win64 (x86_64) 4 | -- 5 | -- Host: localhost Database: atliq_college_db 6 | -- ------------------------------------------------------ 7 | -- Server version 8.0.31 8 | 9 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; 10 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; 11 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; 12 | /*!50503 SET NAMES utf8 */; 13 | /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; 14 | /*!40103 SET TIME_ZONE='+00:00' */; 15 | /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; 16 | /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 17 | /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; 18 | /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; 19 | 20 | -- 21 | -- Table structure for table `fees` 22 | -- 23 | 24 | DROP TABLE IF EXISTS `fees`; 25 | /*!40101 SET @saved_cs_client = @@character_set_client */; 26 | /*!50503 SET character_set_client = utf8mb4 */; 27 | CREATE TABLE `fees` ( 28 | `student_name` varchar(100) NOT NULL, 29 | `semester` int NOT NULL, 30 | `total_fees` decimal(10,2) NOT NULL, 31 | `paid` decimal(10,2) NOT NULL, 32 | `pending` decimal(10,2) NOT NULL, 33 | PRIMARY KEY (`student_name`,`semester`) 34 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 35 | /*!40101 SET character_set_client = @saved_cs_client */; 36 | 37 | -- 38 | -- Dumping data for table `fees` 39 | -- 40 | 41 | LOCK TABLES `fees` WRITE; 42 | /*!40000 ALTER TABLE `fees` DISABLE KEYS */; 43 | INSERT INTO `fees` VALUES ('Bill Gates',2,6000.00,6000.00,0.00),('Elon Musk',3,7000.00,2000.00,5000.00),('Jack Ma',4,8000.00,4000.00,4000.00),('Jeff Bezos',3,7000.00,4000.00,3000.00),('Larry Page',3,7000.00,6000.00,1000.00),('Lauki Lal',1,5000.00,2500.00,2500.00),('Mark Zuckerberg',3,7000.00,5000.00,2000.00),('Michael Dell',4,8000.00,6000.00,2000.00),('Peter Pandey',1,5000.00,3000.00,2000.00),('Robert Downey',2,6000.00,4000.00,2000.00),('Sachin Tendulkar',2,6000.00,2000.00,4000.00),('Satya Nadella',4,8000.00,3000.00,5000.00),('Sergey Brin',4,8000.00,2000.00,6000.00),('Steve Jobs',2,6000.00,5000.00,1000.00),('Sundar Pichai',4,8000.00,5000.00,3000.00),('Thor Hathodawala',1,5000.00,3500.00,1500.00),('Tim Cook',3,7000.00,3000.00,4000.00),('Tom Cruise',2,6000.00,3000.00,3000.00),('Virat Kohli',1,5000.00,4000.00,1000.00),('Wanda Mishra',1,5000.00,2000.00,3000.00); 44 | /*!40000 ALTER TABLE `fees` ENABLE KEYS */; 45 | UNLOCK TABLES; 46 | 47 | -- 48 | -- Table structure for table `marks` 49 | -- 50 | 51 | DROP TABLE IF EXISTS `marks`; 52 | /*!40101 SET @saved_cs_client = @@character_set_client */; 53 | /*!50503 SET character_set_client = utf8mb4 */; 54 | CREATE TABLE `marks` ( 55 | `student_name` varchar(100) NOT NULL, 56 | `semester` int NOT NULL, 57 | `gpa` decimal(3,2) NOT NULL, 58 | PRIMARY KEY (`student_name`,`semester`) 59 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; 60 | /*!40101 SET character_set_client = @saved_cs_client */; 61 | 62 | -- 63 | -- Dumping data for table `marks` 64 | -- 65 | 66 | LOCK TABLES `marks` WRITE; 67 | /*!40000 ALTER TABLE `marks` DISABLE KEYS */; 68 | INSERT INTO `marks` VALUES ('Bill Gates',2,3.00),('Elon Musk',3,2.70),('Jack Ma',4,3.30),('Jeff Bezos',3,2.50),('Larry Page',3,2.90),('Lauki Lal',1,3.60),('Mark Zuckerberg',3,2.80),('Michael Dell',4,3.50),('Peter Pandey',1,3.70),('Robert Downey',2,3.40),('Sachin Tendulkar',2,3.20),('Satya Nadella',4,3.20),('Sergey Brin',4,3.10),('Steve Jobs',2,3.10),('Sundar Pichai',4,3.40),('Thor Hathodawala',1,3.80),('Tim Cook',3,2.60),('Tom Cruise',2,3.30),('Virat Kohli',1,3.90),('Wanda Mishra',1,3.50); 69 | /*!40000 ALTER TABLE `marks` ENABLE KEYS */; 70 | UNLOCK TABLES; 71 | /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; 72 | 73 | /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; 74 | /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; 75 | /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; 76 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; 77 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; 78 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; 79 | /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; 80 | 81 | -- Dump completed on 2023-07-06 18:16:04 82 | -------------------------------------------------------------------------------- /2_openai_functions/db_helper.py: -------------------------------------------------------------------------------- 1 | # Author: Dhaval Patel. Codebasics Inc. 2 | 3 | import mysql.connector 4 | 5 | def get_db_cursor(): 6 | # Open database connection 7 | db = mysql.connector.connect( 8 | host="localhost", 9 | user="root", 10 | password="root", 11 | database="atliq_college_db" 12 | ) 13 | 14 | # Create a new cursor instance 15 | cursor = db.cursor() 16 | 17 | return db, cursor 18 | 19 | 20 | def close_db_connection(db, cursor): 21 | # disconnect from server 22 | cursor.close() 23 | db.close() 24 | 25 | 26 | def get_marks(params): 27 | db, cursor = get_db_cursor() 28 | cursor.callproc('get_marks', [params.get('student_name', ''), params.get('semester', ''), params.get('operation', '')]) 29 | result = None 30 | for res in cursor.stored_results(): 31 | result = float(res.fetchone()[0]) # Fetch the first column of the first row 32 | close_db_connection(db, cursor) 33 | return result 34 | 35 | 36 | def get_fees(params): 37 | db, cursor = get_db_cursor() 38 | cursor.callproc('get_fees', [params.get('student_name', ''), params.get('semester', ''), params.get('fees_type', '')]) 39 | result = None 40 | for res in cursor.stored_results(): 41 | result = float(res.fetchone()[0]) # Fetch the first column of the first row 42 | close_db_connection(db, cursor) 43 | return result 44 | 45 | 46 | if __name__ == "__main__": 47 | print(get_marks({ 48 | 'semester': 4, 49 | 'operation': 'avg' 50 | })) 51 | 52 | print(get_fees({ 53 | 'student_name': "Peter Pandey", 54 | 'semester': 1, 55 | 'fees_type': 'paid' 56 | })) 57 | 58 | -------------------------------------------------------------------------------- /2_openai_functions/image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codebasics/openai-api-tutorials/07e21355480ed311bc0e6f8bd7ecf785fcb3a9fc/2_openai_functions/image.jpg -------------------------------------------------------------------------------- /2_openai_functions/main.py: -------------------------------------------------------------------------------- 1 | # Author: Dhaval Patel. Codebasics Inc. 2 | 3 | import streamlit as st 4 | from openai_helper import get_answer 5 | 6 | st.title("AtliQ College: Q&A System") 7 | 8 | question = st.text_input("Question:") 9 | 10 | if question: 11 | answer = get_answer(question) 12 | st.text("Answer:") 13 | st.write(answer) -------------------------------------------------------------------------------- /2_openai_functions/openai_helper.py: -------------------------------------------------------------------------------- 1 | # Author: Dhaval Patel. Codebasics Inc. 2 | 3 | import openai 4 | import json 5 | from secret_key import openai_api_key 6 | import db_helper 7 | 8 | openai.api_key = openai_api_key 9 | 10 | 11 | def get_answer(question): 12 | messages = [{'role': 'user', 'content': question}] 13 | functions = [ 14 | { 15 | "name": "get_marks", 16 | "description": """Get the GPA for a college student or aggregate GPA (such as average, min, max) 17 | for a given semester. If function returns -1 then it means we could not find the record in a database for given input. 18 | """, 19 | "parameters": { 20 | "type": "object", 21 | "properties": { 22 | "student_name": { 23 | "type": "string", 24 | "description": "First and last Name of the student. e.g John Smith", 25 | }, 26 | "semester": { 27 | "type": "integer", 28 | "description": "A number between 1 to 4 indicating the semester of a student", 29 | }, 30 | "operation": { 31 | "type": "string", 32 | "description": """If student is blank that means aggregate number such as max, min or average is being 33 | requested for an entire semester. semester must be passed in this case. If student field is blank and say 34 | they are passing 1 as a value in semester. Then operation parameter will tell if they need a maximum, minimum 35 | or an average GPA of all students in semester 1. 36 | """, 37 | "enum": ["max", "min", "avg"] 38 | }, 39 | }, 40 | "required": ["semester"], 41 | }, 42 | }, 43 | { 44 | "name": "get_fees", 45 | "description": """Get the fees for an individual student or total fees for an entire 46 | semester. If function returns -1 then it means we could not find the record in a database for given input. 47 | """, 48 | "parameters": { 49 | "type": "object", 50 | "properties": { 51 | "student_name": { 52 | "type": "string", 53 | "description": "First and last Name of the student. e.g John Smith", 54 | }, 55 | "semester": { 56 | "type": "integer", 57 | "description": "A number between 1 to 4 indicating the semester of a student", 58 | }, 59 | "fees_type": { 60 | "type": "string", 61 | "description": "fee type such as 'paid', 'pending' or 'total'", 62 | "enum": ["paid", "pending", "total"] 63 | }, 64 | }, 65 | "required": ["semester"], 66 | }, 67 | } 68 | ] 69 | 70 | response = openai.ChatCompletion.create( 71 | model="gpt-3.5-turbo-0613", 72 | messages=messages, 73 | functions=functions, 74 | function_call="auto", # auto is default, but we'll be explicit 75 | ) 76 | response_message = response["choices"][0]["message"] 77 | 78 | if response_message.get("function_call"): 79 | available_functions = { 80 | "get_marks": db_helper.get_marks, 81 | "get_fees": db_helper.get_fees 82 | } 83 | function_name = response_message["function_call"]["name"] 84 | fuction_to_call = available_functions[function_name] 85 | function_args = json.loads(response_message["function_call"]["arguments"]) 86 | function_response = fuction_to_call(function_args) 87 | 88 | messages.append(response_message) 89 | messages.append( 90 | { 91 | "role": "function", 92 | "name": function_name, 93 | "content": str(function_response), 94 | } 95 | ) 96 | second_response = openai.ChatCompletion.create( 97 | model="gpt-3.5-turbo-0613", 98 | messages=messages, 99 | ) # get a new response from GPT where it can see the function response 100 | return second_response["choices"][0]["message"]["content"] 101 | else: 102 | return response_message["content"] 103 | 104 | 105 | 106 | if __name__ == '__main__': 107 | # print(get_answer("What was Peter Pandey's GPA in semester 1?")) 108 | # print(get_answer("average gpa in third semester?")) 109 | # print(get_answer("how much is peter pandey's pending fees in the first semester?")) 110 | print(get_answer("how much was peter pandey's due fees in the first semester?")) 111 | # print(get_answer("what is the purpose of a balance sheet?")) -------------------------------------------------------------------------------- /2_openai_functions/secret_key.py: -------------------------------------------------------------------------------- 1 | openai_api_key = "add your key here" -------------------------------------------------------------------------------- /2_openai_functions/test_functions.py: -------------------------------------------------------------------------------- 1 | import openai 2 | from secret_key import openai_api_key 3 | import json 4 | 5 | openai.api_key = openai_api_key 6 | 7 | def get_current_weather(location, unit="fahrenheit"): 8 | """Get the current weather in a given location""" 9 | weather_info = { 10 | "location": location, 11 | "temperature": "72", 12 | "unit": unit, 13 | "forecast": ["sunny", "windy"], 14 | } 15 | return json.dumps(weather_info) 16 | 17 | 18 | response_message = openai.ChatCompletion.create( 19 | model="gpt-3.5-turbo", 20 | messages=[ 21 | {"role": "user", "content": "What is the weather like in boston?"} 22 | ], 23 | functions=[ 24 | { 25 | "name": "get_current_weather", 26 | "description": "Get the current weather in a given location", 27 | "parameters": { 28 | "type": "object", 29 | "properties": { 30 | "location": { 31 | "type": "string", 32 | "description": "The city and state, e.g. San Francisco, CA", 33 | }, 34 | "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, 35 | }, 36 | "required": ["location"] 37 | } 38 | } 39 | ] 40 | ) 41 | 42 | if response_message.get('function_call'): 43 | function_name = response_message['function_call']["name"] 44 | function_args = json.loads(response_message['function_call']["arguments"]) 45 | 46 | available_functions = { 47 | 'get_current_weather': get_current_weather 48 | } 49 | 50 | function_to_call = available_functions[function_name] 51 | 52 | function_response = function_to_call( 53 | location=function_args.get("location"), 54 | unit=function_args.get("unit") 55 | ) 56 | 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # openai-api-tutorials 2 | Tutorials for openai-api 3 | --------------------------------------------------------------------------------