This code example is a FastAPI
server that contains multiple API endpoints for interacting with an OpenAI
model.
- 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.
- It uses
HTTPBasic authentication
for security. - It defines endpoints for
health status
, fetching simple text fromOpenAI
, fetching text with a prompt fromOpenAI
, and uploading a file as context for the question to get a response fromOpenAI
. The code also includesOpenAPI
configuration for theSwagger 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
- 2. Setup of the Python environment
- 3. Configure and start the
simple-openai-server
FastAPI server - 4. Get your own OpenAI access
Please follow the link for the installation Download Python for Windows. Note: Additional resources, how to Set virtual environment for Python.
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
.
Please follow the link for the installation oder verification Learn Microsoft Powershell
Please follow the link for the installation of VSCode
Please follow the link for the installation of How to install GitBash
- 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
# 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
- 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"
- 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
http://localhost:8080/docs
- 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}\"}"
Note: Keep in mind OpenAI
has a kind of prepaid model.