Python Fast API integrated with Chat GPT-4.
Python Fast API running on Google Cloud Run container app, and integrated with Chat GPT 4 via the Open AI API.
Please refer below steps to setup the Python Fast API.
- First clone [gh repo clone ravindersirohi/python-fast-api] the python-fast-api repository.
- Go to the python-chat-open-ai folder.
- Run - [pip install -r requirements.txt] from command prompt.
- Now create the virtual environment [python -m venv env] and activate (Windows [.\env\Scripts\activate], macOS/Linux [source env/bin/activate]) the virtual environment.
- [uvicorn main:app --reload] to start the app, once api is up should see http://127.0.0.1:8000
- http://127.0.0.1:8000/docs to see the Open API/Swagger documentation.
Source and infrastructre deployed via the Github workflow, please refer .github/workflow/deploy yaml file.
- Register yourself on Open AI platform and generate Open AI key, required to make a call from any client, in this case Fast API.
- Add OPENAI_API_KEY environment variable in Github Environment variable (Repository settings->Secrets and Variables) and set this Open AI key.
- Populate GCP_PROJECT_ID and GCP_CREDENTIALS in GitHub secrets (Repository settings->Secrets and Variables).
API infrastructe is setup on Google Cloud Platform via Terraform, please refer gcp-terraform-infra and extend for below resources.
Service Account:
resource "google_service_account" "service_account" {
account_id = "service-account-id"
display_name = "Service Account"
}
Artifact Registry:
resource "google_artifact_registry_repository" "my-repo" {
location = "europ-west2"
repository_id = "my-repository"
description = "example docker repository"
format = "DOCKER"
}
Once the source code gets deployed via the Github trigger, should be able to see container access point (for example - https://gcp-chat-gpt-fast-api-xxxxx.europe-west2.run.app). Please follow below to access Chat GPT API.
- .../docs - Swagger UI endpoint.
- .../chat - To interact with chat by sending the below payload.
Post request /chat
{
"prompt": "Who is 007?"
}
If, everything configured correctly should get below response.
{
"prompt": "Who is 007?",
"response": "007 is a fictional character in a series of spy novels written by British author Ian Fleming. The character, also known by his code name, James Bond, is a Secret Service agent working for the British government. He is known for his sophistication, skill in espionage and combat, and his iconic status as a symbol of mid-20th century Cold War intrigue. The character has been adapted and continued in films, television, and other media."
}