As artificial intelligence technology continues to advance, there are many exciting opportunities for developers to create innovative applications using powerful language models like the GPT API. In this post, we will walk through the steps to build a simple question-answering bot using the GPT API and GoLang.
First, let's take a look at the tools we will need for this project. We will be using the OpenAI GPT API, which provides access to one of the most powerful language models available. Additionally, we will be using GoLang, a popular programming language known for its efficiency and ease of use.
STEPS:
To get started, we will need to set up a few things. First, we will need to sign up for the OpenAI API and obtain an API key. (OpenAI API website: https://openai.com/api/ )
This is what OpenAI API dashboard looks like. For creating a new API key, you can click Create new secret key
Create a Go file and import the following dependencies. Do not forget to run "
go get .
" to install the dependencies.import ( "context" "fmt" "log" "os" "bufio" "github.com/PullRequestInc/go-gpt3" "github.com/joho/godotenv" )
You need to create a .env file in the project folder specifying your API key.
API_KEY=<openAI API Key>
Load the API key in your main function.
godotenv.Load() apiKey := os.Getenv("API_KEY") if apiKey == "" { log.Fatalln("Missing API KEY") }
Take iterative question input from the user. Don't forget to wrap everything in an 'infinite for loop' to make it iterative.
fmt.Println("Enter your question"); scanner := bufio.NewScanner(os.Stdin) if scanner.Scan() { question = scanner.Text() }
Create a gpt3 client.
ctx := context.Background() client := gpt3.NewClient(apiKey)
This is the main step. Here we will call the gpts3's completion request.
resp, err := client.CompletionWithEngine(ctx,"davinci", gpt3.CompletionRequest{ Prompt: []string{"<your_prompt>" + question+"?\nA:"}, MaxTokens: gpt3.IntPtr(100), Stop: []string{"\n"}, Echo: false, Temperature: gpt3.Float32Ptr(0), TopP: gpt3.Float32Ptr(1), FrequencyPenalty: *gpt3.Float32Ptr(0), PresencePenalty: *gpt3.Float32Ptr(0), }) if err != nil { log.Fatalln(err) }
Here, We have used gpt3 '
davinci
' model. 'Davinci' is the most capable model family and can perform any task the other models can perform and often with less instruction.Parameters explanation:
max_tokens
: Themax_tokens
parameter sets an upper bound on how many tokens the API will return.prompt
: Theprompt
to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.stop
: Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain thestop
sequence.echo
:echo
back the prompt in addition to the completiontemperature
: What samplingtemperature
to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.top_p
: An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens withtop_p
probability mass. So 0.1 means only the tokeFrequencyPenalty
: Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.PresencePenalty
: Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.In our example, we will use the following prompt in order to train it like a question answers bot (Replace it with
<your_prompt>
):I am a highly intelligent question answering bot. If you ask me a question that is rooted in truth, I will give you the answer. If you ask me a question that is nonsense, trickery, or has no clear answer, I will respond with \"Unknown\".\n\nQ: What is human life expectancy in the United States?\nA: Human life expectancy in the United States is 78 years.\n\nQ: Who was president of the United States in 1955?\nA: Dwight D. Eisenhower was president of the United States in 1955.\n\nQ: Which party did he belong to?\nA: He belonged to the Republican Party.\n\nQ: What is the square root of banana?\nA: Unknown\n\nQ: How does a telescope work?\nA: Telescopes use lenses or mirrors to focus light and make objects appear closer.\n\nQ: Where were the 1992 Olympics held?\nA: The 1992 Olympics were held in Barcelona, Spain.\n\nQ: How many squigs are in a bonk?\nA: Unknown\n\nQ:
Once we receive a response from the GPT API, we can parse the results and extract the most relevant answer to the user's question. We can then return this answer to the user.
fmt.Println(resp.Choices[0].Text)}
Don't forget to add a stop condition before creating the gpt3 client.
if(question=="stop"){ break }
Hurray! Your question answers bot with a tiny code is ready. Now run the code using
go run .
and test your bot. Here is a working example below.
In conclusion, building a question-answering bot using OpenAI's GPT API and GoLang is a great way to showcase the power of modern language models and demonstrate the possibilities of artificial intelligence. With a little bit of coding and some creativity, you can create an exciting and useful CLI application that helps users find the information they need quickly and easily.
You can check the full code here.