Skip to content

Latest commit

 

History

History
90 lines (65 loc) · 2.57 KB

GPT-TODO.md

File metadata and controls

90 lines (65 loc) · 2.57 KB

Advice from ChatGPT-4 on how to use their API to improve random sentences

Given your constraints, a straightforward approach would be to use an external API to handle the text rewriting process. You can use GPT-3, for example, by making API calls from your golang application. Here's a simple example of how to make an API call to the OpenAI GPT-3 API using the net/http package in golang:

  1. First, sign up for an API key from OpenAI (https://beta.openai.com/signup/).

  2. Install the required package for handling JSON:

go get -u github.com/tidwall/gjson
  1. Use the following golang code as a starting point to make API calls to GPT-3:
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"

	"github.com/tidwall/gjson"
)

const (
	openaiAPIKey := os.Getenv("MY_API_KEY")
	openaiAPIURL = "https://api.openai.com/v1/engines/davinci-codex/completions"
)

type prompt struct {
	Prompt string `json:"prompt"`
}

func main() {
	inputSentence := "Rewrite the following sentence in a more meaningful and coherent way: 'Her finished uncle can't manifest'."

	chatGPTRequestData := prompt{
		Prompt: inputSentence,
	}

	requestBody, err := json.Marshal(chatGPTRequestData)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	httpClient := &http.Client{}
	request, err := http.NewRequest("POST", openaiAPIURL, bytes.NewBuffer(requestBody))
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	request.Header.Set("Content-Type", "application/json")
	request.Header.Set("Authorization", "Bearer "+openaiAPIKey)

	response, err := httpClient.Do(request)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	defer response.Body.Close()

	chatGPTResponseBody, err := ioutil.ReadAll(response.Body)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	rewrittenSentence := gjson.Get(string(chatGPTResponseBody), "choices.0.text").String()
	fmt.Println("Input sentence:", inputSentence)
	fmt.Println("Rewritten sentence:", rewrittenSentence)
}

Make sure to replace your_openai_api_key with your actual API key from OpenAI.

This example demonstrates a simple way to call the GPT-3 API to rewrite sentences in a more meaningful and coherent way. The code can be easily integrated into your existing golang application.

Please note that using the GPT-3 API might incur costs, depending on your usage. You can find the pricing details on the OpenAI website: https://openai.com/pricing.


See https://platform.openai.com/docs/api-reference/completions/create

https://platform.openai.com/playground?mode=chat&model=gpt-4