This project is a simple FastAPI-based backend service that generates AI-based text completions using the Groq API. It accepts a user prompt and returns AI-generated text based on the prompt.
- /complete-text Endpoint: Accepts a prompt from the user via a POST request and returns an AI-generated text completion.
- Python 3.7+
- Groq API Key
- FastAPI
- Uvicorn
First, clone the repository to your local machine:
git clone https://github.com/yourusername/ai-text-completion-fastapi.git
cd ai-text-completion-fastapiIt is recommended to use a virtual environment to manage dependencies:
python -m venv env
source env/bin/activate # On Windows: env\Scripts\activateInstall the required Python libraries using the requirements.txt file:
pip install -r requirements.txtYou need to create a .env file to store your API key. Create a file named .env in the root of the project and add the following line:
GROQ_API_KEY=your_groq_api_key_hereReplace your_groq_api_key_here with your actual Groq API key.
Start the FastAPI development server using Uvicorn:
uvicorn main:app --reloadThe server should start at http://127.0.0.1:8000.
You can now test the API using curl or any API client (like Postman). Here's an example using curl:
curl -X POST -H "Content-Type: application/json" -d "{\"prompt\":\"Once upon a time\"}" http://localhost:8000/complete-textThe expected response should look like this:
{
"completion": "Once upon a time, there was a small village in the mountains..."
}main.py: Contains the FastAPI app and the/complete-textendpoint.requirements.txt: Lists the dependencies required for the project..env: Stores your Groq API key (this file is not included in the repository for security reasons).