{"id":20786010,"url":"https://github.com/thomassuedbroecker/simple-openai-fastapi-server","last_synced_at":"2026-03-02T05:01:42.018Z","repository":{"id":208521040,"uuid":"721839745","full_name":"thomassuedbroecker/simple-openai-fastapi-server","owner":"thomassuedbroecker","description":"This code example is a FastAPI server that contains multiple API endpoints for interacting with an OpenAI model.","archived":false,"fork":false,"pushed_at":"2023-11-27T12:51:27.000Z","size":4571,"stargazers_count":8,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-10-08T19:26:36.966Z","etag":null,"topics":["bash","curl","fastapi","openai","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thomassuedbroecker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-21T22:01:34.000Z","updated_at":"2025-09-10T13:05:36.000Z","dependencies_parsed_at":"2023-11-27T13:43:37.236Z","dependency_job_id":null,"html_url":"https://github.com/thomassuedbroecker/simple-openai-fastapi-server","commit_stats":null,"previous_names":["thomassuedbroecker/simple-openai-fastapi-server"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/thomassuedbroecker/simple-openai-fastapi-server","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomassuedbroecker%2Fsimple-openai-fastapi-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomassuedbroecker%2Fsimple-openai-fastapi-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomassuedbroecker%2Fsimple-openai-fastapi-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomassuedbroecker%2Fsimple-openai-fastapi-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thomassuedbroecker","download_url":"https://codeload.github.com/thomassuedbroecker/simple-openai-fastapi-server/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomassuedbroecker%2Fsimple-openai-fastapi-server/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29993029,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-02T01:47:34.672Z","status":"online","status_checked_at":"2026-03-02T02:00:07.342Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","curl","fastapi","openai","python"],"created_at":"2024-11-17T14:50:09.734Z","updated_at":"2026-03-02T05:01:41.993Z","avatar_url":"https://github.com/thomassuedbroecker.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simple OpenAI FastAPI server\n\nThis code example is a [`FastAPI`](https://fastapi.tiangolo.com/) server that contains multiple API endpoints for interacting with an [`OpenAI`](https://openai.com/) model. \n\n* [Related blog post](https://suedbroecker.net/2023/11/23/how-to-create-a-fastapi-server-to-use-openai-models/)\n\n## The objective is to provide three main endpoints\n\n* One endpoint is to send a simple text to Open AI and return the result.\n* 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.\n* One endpoint is to upload a file and question as parameters for Open AI.\n \n## Overview\n\n* It uses [`HTTPBasic authentication`](https://en.wikipedia.org/wiki/Basic_access_authentication) for security. \n* It defines endpoints for `health status`, fetching simple text from [`OpenAI`](https://openai.com/), fetching text with a prompt from `OpenAI`, and uploading a file as context for the question to get a response from `OpenAI`. \nThe code also includes `OpenAPI` configuration for the `Swagger UI`. \n* It uses custom modules for environment variables, response and payload definition, and AI access. \n* Finally, it runs the FastAPI application using [`uvicorn`](https://www.uvicorn.org/) on localhost port 8080.\n\n_Clone the project to your local computer:_\n\n```sh\ngit clone https://github.com/thomassuedbroecker/simple-openai-server-fastapi.git\n```\n\n![](/images/2023-11-22_fastapi-01.gif)\n\n_Note:_ \n\n  * 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.\n  * You can also use the Online editor (github.**dev**) to edit files: `https://github.dev/thomassuedbroecker/simple-openai-server-fastapi`.\n\n_Content_\n\n* [1. Setup of the Windows Machine](#1-setup-windows-machine)\n* [2. Setup of the Python environment](#2-setup-the-python-environment)\n* [3. Configure and start the `simple-openai-server` FastAPI server](#3-configure-and-start-the-simple-openai-server-fastapi-server)\n* [4. Get your own OpenAI access](#4-get-your-own-openai-access)\n\n## 1. Setup of the Windows Machine\n\n### 1.1 Install Python\n\nPlease follow the link for the installation [Download Python for Windows](https://www.python.org/downloads/windows/).\n_Note:_ Additional resources, how to [Set virtual environment for Python](https://suedbroecker.net/2023/05/23/set-a-virtual-environment-for-python/).\n\n### 1.2 Install virtual environment\n\nFollow the steps in [set up a virtual environment for Python](https://suedbroecker.net/2023/05/23/set-a-virtual-environment-for-python/)\n\n_Note:_ To add the `path variable` please open in your Windows search bar `Edit environment variables for your account`.\n \n### 1.3 Ensure you can use PowerShell on Windows\n\nPlease follow the link for the installation oder verification [Learn Microsoft Powershell](https://learn.microsoft.com/en-us/powershell/scripting/windows-powershell/starting-windows-powershell?view=powershell-7.3)\n\n### 1.4 Install VSCode\n\nPlease follow the link for the installation of [`VSCode`](https://code.visualstudio.com/)\n\n### 1.5 Install GitBash\n\nPlease follow the link for the installation of [`How to install GitBash`](https://www.educative.io/answers/how-to-install-git-bash-in-windows)\n\n## 2. Setup of the Python environment\n\n### 2.1 Create a virtual Python environment\n\n* Windows with GitBash terminal.\n\n```sh\ncd code\npython3.10 -m venv env3.10\nsource ./env3.10/Scripts/activate\n```\n\n* Mac and Linux terminal.\n\n```sh\ncd code\npython3.10 -m venv env3.10\nsource ./env3.10/bin/activate\n```\n\n### 2.2. Install the needed Python libraries \n\n```sh\n# Linux\n#source ./env3.10/bin/activate \n# Windows\nsource ./env3.10/Scripts/activate \npython3 -m pip install --upgrade pip\npython3 -m pip install \"fastapi[all]\"\npython3 -m pip install requests\npython3 -m pip install pydantic\npython3 -m pip install openai\npython3 -m pip install typing\npython3 -m pip install beautifulsoup4\npython3 -m pip install --upgrade openai\npython3 -m pip freeze \u003e requirements.txt \n```\n\n## 3. Configure and start the `simple-openai-server` FastAPI server\n\n### 3.1 Create the environment file\n\n* Set a new user and password.\n\n```sh\ncat .env_template \u003e .env\n```\n* Content\n\n```sh\n# APP\nexport APP_USER=admin\nexport APP_APIKEY=admin\n\n# OpenAI\nexport OPENAI_KEY=YOUR_KEY\nexport OPENAI_MODEL=gpt-3.5-turbo-1106\nexport PROMPT=\"Document:\\n\\n\u003c\u003cCONTEXT\u003e\u003e\\n\\nQuestion:\\n\\n\u003c\u003cQUESTION\u003e\u003e\\n\\nAnswer:\\n\\n\"\n```\n\n### 3.2 Start the `simple-openai-server` FastAPI server\n\n* Windows \n\n```sh\ncd code\nsource ./env3.10/Scripts/activate \nsource .env\npython3 simple-openai-server.py\n```\n\n* Linux\n\n```sh\ncd code\nsource ./env3.10/bin/activate \nsource .env\npython3 simple-openai-server.py\n```\n\n### 3.3 Open a browser and enter the following URL\n\n```sh\nhttp://localhost:8080/docs\n```\n\n### 3.4 Invoke FastAPI server endpoints by using the curl commands\n\n* Access FastAPIserver `/`\n\n```sh\nexport URL=http://localhost:8080\ncurl -X GET \\\n  \"${URL}/\" \\\n  -H \"Content-Type: application/json\" \n```\n\n* Using REST GET to invoke the `health` endpoint\n\n```sh\nexport URL=http://localhost:8080\nexport USER=admin\nexport PASSWORD=thomas\nexport REST_API_PATH=health\ncurl -u ${USER}:${PASSWORD} -X GET \"${URL}/${REST_API_PATH}\"\n```\n\n* Using REST POST to invoke the `get_openai_text_with_prompt` endpoint.\n\n```sh\nexport URL=http://localhost:8080\nexport USER=admin\nexport PASSWORD=thomas\nexport CONTEXT=\"My name is Thomas.\"\nexport QUESTION=\"What is my name?\"\nexport REST_API_PATH=get_openai_text_with_prompt\n\ncurl -u ${USER}:${PASSWORD} -X POST \"${URL}/${REST_API_PATH}/\" -H \"Content-Type: application/json\" -d \"{\\\"context\\\":\\\"${CONTEXT}\\\",\\\"question\\\":\\\"${QUESTION}\\\"}\"\n```\n\n## 4. Get your own OpenAI access\n\n### 4.1 Register your account\n\n_Note:_ Keep in mind `OpenAI` has a kind of prepaid model.\n\n* [OpenAI](https://openai.com/blog/openai-api)\n* [OpenAI Pricing](https://openai.com/pricing)\n\n## 5. Additional notes\n\n* [Files](https://fastapi.tiangolo.com/tutorial/request-files/)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomassuedbroecker%2Fsimple-openai-fastapi-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthomassuedbroecker%2Fsimple-openai-fastapi-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomassuedbroecker%2Fsimple-openai-fastapi-server/lists"}