|
1 |
| -# Python project template |
| 1 | +# Python OpenAI demos |
2 | 2 |
|
3 |
| -This is a template repository for any Python project that comes with the following dev tools: |
| 3 | +This repository contains a collection of Python scripts that demonstrate how to use the OpenAI API to generate chat completions. |
4 | 4 |
|
5 |
| -* `ruff`: identifies many errors and style issues (`flake8`, `isort`, `pyupgrade`) |
6 |
| -* `black`: auto-formats code |
| 5 | +In increasing order of complexity, the scripts are: |
7 | 6 |
|
8 |
| -Those checks are run as pre-commit hooks using the `pre-commit` library. |
| 7 | +1. [`chat.py`](./chat.py): A simple script that demonstrates how to use the OpenAI API to generate chat completions. |
| 8 | +2. [`chat_stream.py`](./chat_stream.py): Adds `stream=True` to the API call to return a generator that streams the completion as it is being generated. |
| 9 | +3. [`chat_history.py`](./chat_history.py): Adds a back-and-forth chat interface using `input()` which keeps track of past messages and sends them with each chat completion call. |
| 10 | +4. [`chat_history_stream.py`](./chat_history_stream.py): The same idea, but with `stream=True` enabled. |
9 | 11 |
|
10 |
| -It includes `pytest` for testing plus the `pytest-cov` plugin to measure coverage. |
| 12 | +Plus these scripts to demonstrate additional features: |
11 | 13 |
|
12 |
| -The checks and tests are all run using Github actions on every pull request and merge to main. |
| 14 | +5. [`chat_safety.py`](./chat_safety.py): The simple script with exception handling for Azure AI Content Safety filter errors. |
| 15 | +6. [`chat_async.py`](./chat_async.py): Uses the async clients to make asynchronous calls, including an example of sending off multiple requests at once using `asyncio.gather`. |
13 | 16 |
|
14 |
| -This repository is setup for Python 3.11. To change the version: |
15 |
| -1. Change the `image` argument in `.devcontainer/devcontainer.json` (see [https://github.com/devcontainers/images/tree/main/src/python](https://github.com/devcontainers/images/tree/main/src/python#configuration) for a list of pre-built Docker images) |
16 |
| -1. Change the config options in `.precommit-config.yaml` |
17 |
| -1. Change the version number in `.github/workflows/python.yaml` |
| 17 | +## Setting up the environment |
18 | 18 |
|
19 |
| -## Development instructions |
| 19 | +If you open this up in a Dev Container or GitHub Codespaces, everything will be setup for you. |
| 20 | +If not, follow these steps: |
20 | 21 |
|
21 |
| -## With devcontainer |
| 22 | +1. Set up a Python virtual environment and activate it. |
22 | 23 |
|
23 |
| -This repository comes with a devcontainer (a Dockerized Python environment). If you open it in Codespaces, it should automatically initialize the devcontainer. |
| 24 | +2. Install the required packages: |
24 | 25 |
|
25 |
| -Locally, you can open it in VS Code with the Dev Containers extension installed. |
26 |
| - |
27 |
| -## Without devcontainer |
28 |
| - |
29 |
| -If you can't or don't want to use the devcontainer, then you should first create a virtual environment: |
30 |
| - |
31 |
| -``` |
32 |
| -python3 -m venv .venv |
33 |
| -source .venv/bin/activate |
34 |
| -``` |
35 |
| - |
36 |
| -Then install the dev tools and pre-commit hooks: |
37 |
| - |
38 |
| -``` |
39 |
| -python3 -m pip install --user -r requirements-dev.txt |
40 |
| -pre-commit install |
| 26 | +```bash |
| 27 | +pip install -r requirements.txt |
41 | 28 | ```
|
42 | 29 |
|
43 |
| -## Adding code and tests |
| 30 | +## Configuring the OpenAI environment variables |
44 | 31 |
|
45 |
| -This repository starts with a very simple `main.py` and a test for it at `tests/main_test.py`. |
46 |
| -You'll want to replace that with your own code, and you'll probably want to add additional files |
47 |
| -as your code grows in complexity. |
| 32 | +These scripts can be run against an Azure OpenAI account, an OpenAI.com account, or a local Ollama server, |
| 33 | +depending on the environment variables you set. |
48 | 34 |
|
49 |
| -When you're ready to run tests, run: |
| 35 | +1. Copy the `.env.sample file to a new file called `.env`: |
50 | 36 |
|
51 |
| -``` |
52 |
| -python3 -m pytest |
53 |
| -``` |
| 37 | + ```bash |
| 38 | + cp .env.sample .env |
| 39 | + ``` |
54 | 40 |
|
55 |
| -# File breakdown |
| 41 | +2. For Azure OpenAI, create an Azure OpenAI gpt-3.5 or gpt-4 deployment, and customize the `.env` file with your Azure OpenAI endpoint and deployment id. |
56 | 42 |
|
57 |
| -Here's a short explanation of each file/folder in this template: |
| 43 | + ```bash |
| 44 | + API_HOST=azure |
| 45 | + AZURE_OPENAI_ENDPOINT=https://YOUR-AZURE-OPENAI-SERVICE-NAME.openai.azure.com |
| 46 | + AZURE_OPENAI_DEPLOYMENT=YOUR-AZURE-DEPLOYMENT-NAME |
| 47 | + AZURE_OPENAI_VERSION=2024-03-01-preview |
| 48 | + ``` |
58 | 49 |
|
59 |
| -* `.devcontainer`: Folder containing files used for setting up a devcontainer |
60 |
| - * `devcontainer.json`: File configuring the devcontainer, includes VS Code settings |
61 |
| -* `.github`: Folder for Github-specific files and folders |
62 |
| - * `workflows`: Folder containing Github actions config files |
63 |
| - * `python.yaml`: File configuring Github action that runs tools and tests |
64 |
| -* `tests`: Folder containing Python tests |
65 |
| - * `main_test.py`: File with pytest-style tests of main.py |
66 |
| -* `.gitignore`: File describing what file patterns Git should never track |
67 |
| -* `.pre-commit-config.yaml`: File listing all the pre-commit hooks and args |
68 |
| -* `main.py`: The main (and currently only) Python file for the program |
69 |
| -* `pyproject.toml`: File configuring most of the Python dev tools |
70 |
| -* `README.md`: You're reading it! |
71 |
| -* `requirements-dev.txt`: File listing all PyPi packages required for development |
72 |
| -* `requirements.txt`: File listing all PyPi packages required for production |
| 50 | +3. For OpenAI.com, customize the `.env` file with your OpenAI API key and desired model name. |
73 | 51 |
|
74 |
| -For a longer explanation, read [this blog post](http://blog.pamelafox.org/2022/09/how-i-setup-python-project.html). |
| 52 | + ```bash |
| 53 | + API_HOST=openai |
| 54 | + OPENAI_KEY=YOUR-OPENAI-API-KEY |
| 55 | + OPENAI_MODEL=gpt-3.5-turbo |
| 56 | + ``` |
75 | 57 |
|
76 |
| -# 🔎 Found an issue or have an idea for improvement? |
| 58 | +4. For Ollama, customize the `.env` file with your Ollama endpoint and model name (any model you've pulled). |
77 | 59 |
|
78 |
| -Help me make this template repository better by letting us know and opening an issue! |
| 60 | + ```bash |
| 61 | + API_HOST=ollama |
| 62 | + OLLAMA_ENDPOINT=http://localhost:11434/v1 |
| 63 | + OLLAMA_MODEL=llama2 |
| 64 | + ``` |
0 commit comments