-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update file paths for resources
- Loading branch information
Showing
11 changed files
with
142 additions
and
9 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
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,133 @@ | ||
# API Package Documentation | ||
|
||
This api package provides core types, request/response models, and utilities for interacting with the ChatGPT CLI’s API. It includes data structures for managing completions, messages, and error responses, as well as supporting JSON marshaling behavior. | ||
|
||
## Package Structure | ||
```shell | ||
api | ||
├── client # Client interface for handling API calls | ||
├── completions.go # Structures for completions API requests/responses | ||
├── http # HTTP client for network requests | ||
└── models.go # Data models for ChatGPT API | ||
``` | ||
|
||
## Overview | ||
|
||
The api package contains types and functions to manage communication with the ChatGPT API. Key components include: | ||
|
||
* Data Models: Defines Message, CompletionsRequest, CompletionsResponse, and other structures for API data handling. | ||
* Client and HTTP Layer: The client and http sub-packages implement the API client and HTTP requests, respectively. | ||
* Custom Types: Includes custom JSON marshalers for fine-grained control of the API response structure. | ||
|
||
### Core Data Structures | ||
|
||
#### CompletionsRequest | ||
|
||
The CompletionsRequest struct models the parameters for making a completion request to the API. | ||
|
||
```go | ||
type CompletionsRequest struct { | ||
Model string `json:"model"` | ||
Temperature float64 `json:"temperature"` | ||
TopP float64 `json:"top_p"` | ||
FrequencyPenalty float64 `json:"frequency_penalty,omitempty"` | ||
MaxTokens int `json:"max_tokens"` | ||
PresencePenalty float64 `json:"presence_penalty,omitempty"` | ||
Messages []Message `json:"messages"` | ||
Stream bool `json:"stream"` | ||
Seed int `json:"seed,omitempty"` | ||
} | ||
``` | ||
|
||
* Model: Specifies the model to use. | ||
* Temperature and TopP: Control the response variability. | ||
* Messages: The conversation history to provide context for responses. | ||
|
||
### CompletionsResponse | ||
|
||
The CompletionsResponse struct represents a response from the API. | ||
|
||
```go | ||
type CompletionsResponse struct { | ||
ID string `json:"id"` | ||
Object string `json:"object"` | ||
Created int `json:"created"` | ||
Model string `json:"model"` | ||
Usage Usage `json:"usage"` | ||
Choices []Choice `json:"choices"` | ||
} | ||
``` | ||
|
||
#### Message | ||
|
||
Represents a message in a conversation, including the role and content. | ||
```go | ||
type Message struct { | ||
Role string `json:"role"` | ||
Content string `json:"content"` | ||
} | ||
``` | ||
|
||
* Role: The role of the message (e.g., user, assistant). | ||
* Content: The actual text content of the message. | ||
|
||
#### Error Handling | ||
|
||
The ErrorResponse struct models errors returned by the API: | ||
```go | ||
type ErrorResponse struct { | ||
Error struct { | ||
Message string `json:"message"` | ||
Type string `json:"type"` | ||
Code string `json:"code"` | ||
} `json:"error"` | ||
} | ||
``` | ||
|
||
Errors include fields for message, type, and code, allowing structured error handling. | ||
|
||
#### Custom JSON Marshalling | ||
|
||
The Float64 type is a custom wrapper around float64 with special JSON marshalling behavior, which omits the field if the value is zero. | ||
|
||
```go | ||
type Float64 float64 | ||
|
||
func (f Float64) MarshalJSON() ([]byte, error) { | ||
if f == 0.0 { | ||
return []byte("null"), nil | ||
} | ||
return json.Marshal(float64(f)) | ||
} | ||
``` | ||
|
||
## HTTP Client (api/http) | ||
|
||
The http package provides a client for making HTTP requests, allowing integration with the ChatGPT API. | ||
|
||
### RestCaller | ||
|
||
The RestCaller struct handles POST and GET requests, manages error responses, and processes streamed responses. | ||
|
||
### Usage | ||
|
||
To make a completion request: | ||
```go | ||
client := api.NewClient(config) | ||
request := api.CompletionsRequest{ | ||
Model: "gpt-3.5-turbo", | ||
Messages: []api.Message{ | ||
{ | ||
Role: "user", Content: "Hello, ChatGPT!" | ||
}, | ||
}, | ||
} | ||
|
||
response, err := client.Query(request) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println(response) | ||
``` | ||
|
||
This setup initializes the API client, creates a completion request, and processes the response. |
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
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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