Skip to content

This code example is a FastAPI server that contains multiple API endpoints for interacting with an OpenAI model.

License

Notifications You must be signed in to change notification settings

terrencestella/simple-openai-fastapi-server

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple OpenAI FastAPI server

This code example is a FastAPI server that contains multiple API endpoints for interacting with an OpenAI model.

The objective is to provide three main endpoints

  • One endpoint is to send a simple text to Open AI and return the result.
  • One endpoint to send a preconfigured prompt to Open AI. The preconfigured prompt contains a context and a question as parameters. These parameters will be replaced by the provided values of the endpoint invocation and sent to Open AI. The response will be provided as the return value of the endpoint.
  • One endpoint is to upload a file and question as parameters for Open AI.

Overview

  • It uses HTTPBasic authentication for security.
  • It defines endpoints for health status, fetching simple text from OpenAI, fetching text with a prompt from OpenAI, and uploading a file as context for the question to get a response from OpenAI. The code also includes OpenAPI configuration for the Swagger UI.
  • It uses custom modules for environment variables, response and payload definition, and AI access.
  • Finally, it runs the FastAPI application using uvicorn on localhost port 8080.

Clone the project to your local computer:

git clone https://github.com/thomassuedbroecker/simple-openai-server-fastapi.git

Note:

  • You can find additional information, on how to create a pipeline in the related project: “How to use and set up Watsonx.ai in the simple pipeline project”: https://github.com/thomassuedbroecker/simple-qa-pipeline. We reuse code and concepts from that pipeline project.
  • You can also use the Online editor (github.dev) to edit files: https://github.dev/thomassuedbroecker/simple-openai-server-fastapi.

Content

1. Setup of the Windows Machine

1.1 Install Python

Please follow the link for the installation Download Python for Windows. Note: Additional resources, how to Set virtual environment for Python.

1.2 Install virtual environment

Follow the steps in set up a virtual environment for Python

Note: To add the path variable please open in your Windows search bar Edit environment variables for your account.

1.3 Ensure you can use PowerShell on Windows

Please follow the link for the installation oder verification Learn Microsoft Powershell

1.4 Install VSCode

Please follow the link for the installation of VSCode

1.5 Install GitBash

Please follow the link for the installation of How to install GitBash

2. Setup of the Python environment

2.1 Create a virtual Python environment

  • Windows with GitBash terminal.
cd code
python3.10 -m venv env3.10
source ./env3.10/Scripts/activate
  • Mac and Linux terminal.
cd code
python3.10 -m venv env3.10
source ./env3.10/bin/activate

2.2. Install the needed Python libraries

# Linux
#source ./env3.10/bin/activate 
# Windows
source ./env3.10/Scripts/activate 
python3 -m pip install --upgrade pip
python3 -m pip install "fastapi[all]"
python3 -m pip install requests
python3 -m pip install pydantic
python3 -m pip install openai
python3 -m pip install typing
python3 -m pip install beautifulsoup4
python3 -m pip install --upgrade openai
python3 -m pip freeze > requirements.txt 

3. Configure and start the simple-openai-server FastAPI server

3.1 Create the environment file

  • Set a new user and password.
cat .env_template > .env
  • Content
# APP
export APP_USER=admin
export APP_APIKEY=admin

# OpenAI
export OPENAI_KEY=YOUR_KEY
export OPENAI_MODEL=gpt-3.5-turbo-1106
export PROMPT="Document:\n\n<<CONTEXT>>\n\nQuestion:\n\n<<QUESTION>>\n\nAnswer:\n\n"

3.2 Start the simple-openai-server FastAPI server

  • Windows
cd code
source ./env3.10/Scripts/activate 
source .env
python3 simple-openai-server.py
  • Linux
cd code
source ./env3.10/bin/activate 
source .env
python3 simple-openai-server.py

3.3 Open a browser and enter the following URL

http://localhost:8080/docs

3.4 Invoke FastAPI server endpoints by using the curl commands

  • Access FastAPIserver /
export URL=http://localhost:8080
curl -X GET \
  "${URL}/" \
  -H "Content-Type: application/json" 
  • Using REST GET to invoke the health endpoint
export URL=http://localhost:8080
export USER=admin
export PASSWORD=thomas
export REST_API_PATH=health
curl -u ${USER}:${PASSWORD} -X GET "${URL}/${REST_API_PATH}"
  • Using REST POST to invoke the get_openai_text_with_prompt endpoint.
export URL=http://localhost:8080
export USER=admin
export PASSWORD=thomas
export CONTEXT="My name is Thomas."
export QUESTION="What is my name?"
export REST_API_PATH=get_openai_text_with_prompt

curl -u ${USER}:${PASSWORD} -X POST "${URL}/${REST_API_PATH}/" -H "Content-Type: application/json" -d "{\"context\":\"${CONTEXT}\",\"question\":\"${QUESTION}\"}"

4. Get your own OpenAI access

4.1 Register your account

Note: Keep in mind OpenAI has a kind of prepaid model.

5. Additional notes

About

This code example is a FastAPI server that contains multiple API endpoints for interacting with an OpenAI model.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%