Skip to content

Commit

Permalink
#29: openai client
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrueger12 committed Dec 17, 2023
1 parent 86c624c commit d5e90be
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions pkg/providers/openai/openai.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package openai

import (
"fmt"
"net/http"
"io"
"bytes"
)

type OpenAiClient struct {
apiKey string
baseURL string
http *http.Client
}

func NewOpenAiClient(apiKey string) *OpenAiClient {
return &OpenAiClient{
apiKey: apiKey,
baseURL: "https://api.openai.com/v1",
http: http.DefaultClient,
}
}

func (c *OpenAiClient) SetBaseURL(baseURL string) {
c.baseURL = baseURL
}

func (c *OpenAiClient) SetHTTPOpenAiClient(httpOpenAiClient *http.Client) {
c.http = httpOpenAiClient
}

func (c *OpenAiClient) GetAPIKey() string {
return c.apiKey
}

func (c *OpenAiClient) Get(endpoint string) (string, error) {
// Implement the logic to make a GET request to the OpenAI API

return "", nil
}

func (c *OpenAiClient) Post(endpoint string, payload []byte) (string, error) {
// Implement the logic to make a POST request to the OpenAI API

// Create the full URL
url := c.baseURL + endpoint

// Create a new request using http
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if err != nil {
return "", err
}

// Set the headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+c.apiKey)

// Send the request using http Client
resp, err := c.http.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()

responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}

return string(responseBody), nil
}

// Add more methods to interact with OpenAI API

func main() {
// Example usage of the OpenAI OpenAiClient
OpenAiClient := NewOpenAiClient("YOUR_API_KEY")

// Call methods on the OpenAiClient to interact with the OpenAI API
// For example:
response, err := OpenAiClient.Get("/endpoints")
if err != nil {
fmt.Println("Error:", err)
return
}

fmt.Println("Response:", response)
}

0 comments on commit d5e90be

Please sign in to comment.