-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86c624c
commit d5e90be
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |