This project documentation is also available in Portuguese.
GPT Translate API is a simple application that utilizes the OpenAI API to translate texts between different languages. The API receives a text, the source language, and the target language, and returns the translation generated by the GPT-3.5 Turbo model.
gpt-translate-api/
│
├── .env # Environment variables (keys and sensitive configurations)
├── Makefile # Shortcuts for running commands (build, run, etc.)
├── go.mod # Go module and dependencies
├── go.sum # Dependency checksums
├── index.go # Main initialization file
└── src/
├── config/ # Application configurations
├── handlers/ # Translation processing logic
├── models/ # Data structures and models
├── routes/ # API route definitions and setup using Gin
└── services/ # Functions to interact with OpenAI API
- Go (version 1.20+)
- Postman or another tool to test APIs
- An OpenAI account and a valid API key
-
Clone the repository:
git clone https://github.com/tiagoolivv/gpt-translate-api.git cd gpt-translate-api
-
Install dependencies:
go mod tidy
-
Create the
.env
file with the following variables:OPENAI_MODEL=gpt-3.5-turbo OPENAI_AUTHORIZATION=your_openai_api_key OPENAI_ORGANIZATION=your_organization_id OPENAI_PROJECT=your_project_id OPENAI_URL=https://api.openai.com/v1/chat/completions PORT=8080
-
Run the project:
Using
go run
:go run ./index.go
Or with the
Makefile
:make run
- Method:
POST
- URL:
http://localhost:8080/translate
{
"q": "Hello, how are you?",
"source": "en",
"target": "es"
}
Field | Type | Description |
---|---|---|
q |
string | The text to be translated |
source |
string | The source language code (ISO 639-1) |
target |
string | The target language code (ISO 639-1) |
curl -X POST http://localhost:8080/translate \
-H "Content-Type: application/json" \
-d '{
"q": "Hello, how are you?",
"source": "en",
"target": "es"
}'
{
"text": "Hello, how are you?",
"source": "en",
"target": "es",
"result": "Hola, ¿cómo estás?"
}
You can test the API using Postman or the curl
command as shown above.
- Go - Programming language
- Gin - Web framework for Go
- Resty - HTTP client for Go
- OpenAI API - API for translation using GPT-3.5 Turbo
- Fork the project
- Create a new branch:
git checkout -b feature/your-feature
- Make your changes and commit:
git commit -m 'Added my feature'
- Push to the branch:
git push origin feature/your-feature
- Open a Pull Request