Giter Site home page Giter Site logo

python-openai-demos's Introduction

Python OpenAI demos

This repository contains a collection of Python scripts that demonstrate how to use the OpenAI API to generate chat completions.

In increasing order of complexity, the scripts are:

  1. chat.py: A simple script that demonstrates how to use the OpenAI API to generate chat completions.
  2. chat_stream.py: Adds stream=True to the API call to return a generator that streams the completion as it is being generated.
  3. 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.
  4. chat_history_stream.py: The same idea, but with stream=True enabled.

Plus these scripts to demonstrate additional features:

  1. chat_safety.py: The simple script with exception handling for Azure AI Content Safety filter errors.
  2. chat_async.py: Uses the async clients to make asynchronous calls, including an example of sending off multiple requests at once using asyncio.gather.

Setting up the environment

If you open this up in a Dev Container or GitHub Codespaces, everything will be setup for you. If not, follow these steps:

  1. Set up a Python virtual environment and activate it.

  2. Install the required packages:

pip install -r requirements.txt

Configuring the OpenAI environment variables

These scripts can be run against an Azure OpenAI account, an OpenAI.com account, or a local Ollama server, depending on the environment variables you set.

  1. Copy the .env.sample file to a new file called .env`:

    cp .env.sample .env
  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.

    API_HOST=azure
    AZURE_OPENAI_ENDPOINT=https://YOUR-AZURE-OPENAI-SERVICE-NAME.openai.azure.com
    AZURE_OPENAI_DEPLOYMENT=YOUR-AZURE-DEPLOYMENT-NAME
    AZURE_OPENAI_VERSION=2024-03-01-preview
  3. For OpenAI.com, customize the .env file with your OpenAI API key and desired model name.

    API_HOST=openai
    OPENAI_KEY=YOUR-OPENAI-API-KEY
    OPENAI_MODEL=gpt-3.5-turbo
  4. For Ollama, customize the .env file with your Ollama endpoint and model name (any model you've pulled).

    API_HOST=ollama
    OLLAMA_ENDPOINT=http://localhost:11434/v1
    OLLAMA_MODEL=llama2

python-openai-demos's People

Contributors

pamelafox avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.