Dino Geek, try to help you

How to connect to ChatGPT API with Go?


To connect to the ChatGPT API with Go language, you would need to make network requests to the API endpoint. Here is an example of how to do it:

```
package main

import ( “bytes“ “encoding/json“ “io/ioutil“ “log“ “net/http“
)

// Structs to hold request structure
type Message struct { Role string `json:“role”` Content string `json:“content”`
}

type ChatRequest struct { Messages []Message `json:“messages”`
}

func main() { messages := []Message{{Role: “system”, Content: “You are a helpful assistant.”}, {Role: “user”, Content: “Who won world series in 2020?”}} chatRequest := ChatRequest{Messages: messages} requestBody, err := json.Marshal(chatRequest) if err != nil { log.Fatalln(err) return }

request, err := http.NewRequest(“POST”, “https://api.openai.com/v1/engines/davinci-codex/completions”, bytes.NewBuffer(requestBody)) request.Header.Set(“Authorization”, “Bearer YOUR_OPEN_AI_KEY”) request.Header.Set(“Content-Type”, “application/json”) client := &http.Client{} response, err := client.Do(request) if err != nil { log.Fatalln(err) return } defer response.Body.Close() body, err := ioutil.ReadAll(response.Body) if err != nil { log.Fatalln(err) return } log.Println(string(body)) } ```

Important Note:

Before running the code, please make sure to replace `YOUR_OPEN_AI_KEY` with your own key from OpenAI.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use