The Summarize API
is a simple HTTP-based microservice built using the Go programming language and the Gin web framework. It takes a block of text as input and returns a summarized version of the text by calling OpenAI's GPT-based API (e.g., text-davinci-002
).
This API is designed for use in environments where automatic summarization of text is required. The service accepts JSON input and returns JSON output.
Before running the application, ensure you have the following installed on your machine:
- Go (version 1.16 or higher)
- Gin web framework
- An OpenAI API key
You will need to install the dependencies specified in the code. You can do this using go get
:
go get github.com/gin-gonic/gin
Ensure you set your OpenAI API key as an environment variable, as the service requires it to make requests to the OpenAI API.
export OPENAI_API_KEY=your_openai_api_key
Alternatively, you can add this to your .bashrc
, .zshrc
, or other shell configuration files to make it permanent.
Run the Go application using the following command:
go run main.go
By default, the application runs on http://localhost:8080
.
This endpoint accepts a JSON payload with a block of text and returns a summarized version.
- Method:
POST
- URL:
http://localhost:8080/summarize
- Headers:
Content-Type: application/json
- Body:
{ "text": "Your text to be summarized here." }
- Status Code:
200 OK
- Content-Type:
application/json
- Body:
{ "summary": "This is a summarized version of the text." }
-
400 Bad Request: Returned if the input JSON is invalid.
{ "error": "Invalid JSON format or missing 'text' field." }
-
500 Internal Server Error: Returned if there is an error interacting with the OpenAI API.
{ "error": "OPENAI_API_KEY environment variable is not set" }
-
SummarizeRequest
: This struct defines the expected input payload. It contains atext
field, which holds the text to be summarized. -
SummarizeResponse
: This struct defines the output payload, which contains the summarized text under thesummary
field. -
main()
: Initializes the Gin server and sets up the/summarize
route. -
summarizeHandler()
: The request handler for the/summarize
endpoint. It handles parsing the input JSON, calling thesummarizeText()
function, and returning the response. -
summarizeText()
: This function interacts with the OpenAI API to generate a summary for the provided text. It uses an environment variable (OPENAI_API_KEY
) to authenticate with OpenAI.
- The API request to OpenAI includes:
- Model:
"text-davinci-002"
- Prompt: A custom text prompt requesting a summary of the provided text.
- Max Tokens: 250 tokens to limit the length of the summary.
- Temperature: A temperature of
0.5
for balanced creativity.
- Model:
You can test the API using tools like curl
or Postman.
curl -X POST http://localhost:8080/summarize \
-H "Content-Type: application/json" \
-d '{"text": "Insert a long piece of text here."}'
- Invalid Input: If the input payload is not valid JSON or the required
text
field is missing, a400 Bad Request
response is returned. - Missing API Key: If the
OPENAI_API_KEY
environment variable is not