Skip to content

TiagoOlivv/gpt-translate-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GPT Translate API

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.

Project Structure

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

Prerequisites

  • Go (version 1.20+)
  • Postman or another tool to test APIs
  • An OpenAI account and a valid API key

Installation

  1. Clone the repository:

    git clone https://github.com/tiagoolivv/gpt-translate-api.git
    cd gpt-translate-api
  2. Install dependencies:

    go mod tidy
  3. 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
  4. Run the project:

    Using go run:

    go run ./index.go

    Or with the Makefile:

    make run

Usage

Endpoint: /translate

  • Method: POST
  • URL: http://localhost:8080/translate

Request Body

{
  "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)

Request Example:

curl -X POST http://localhost:8080/translate \
  -H "Content-Type: application/json" \
  -d '{
    "q": "Hello, how are you?",
    "source": "en",
    "target": "es"
  }'

Response Example:

{
  "text": "Hello, how are you?",
  "source": "en",
  "target": "es",
  "result": "Hola, ¿cómo estás?"
}

Testing

You can test the API using Postman or the curl command as shown above.

Technologies Used

  • Go - Programming language
  • Gin - Web framework for Go
  • Resty - HTTP client for Go
  • OpenAI API - API for translation using GPT-3.5 Turbo

How to Contribute

  1. Fork the project
  2. Create a new branch: git checkout -b feature/your-feature
  3. Make your changes and commit: git commit -m 'Added my feature'
  4. Push to the branch: git push origin feature/your-feature
  5. Open a Pull Request

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published