Skip to content

Commit

Permalink
chore: update file paths for resources
Browse files Browse the repository at this point in the history
  • Loading branch information
kardolus committed Nov 6, 2024
1 parent 94892f4 commit 5b4e141
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
ChatGPT CLI provides a powerful command-line interface for seamless interaction with ChatGPT models via OpenAI and
Azure, featuring streaming capabilities and extensive configuration options.

![a screenshot](resources/vhs.gif)
![a screenshot](cmd/chatgpt/resources/vhs.gif)

## Table of Contents

Expand Down
133 changes: 133 additions & 0 deletions api/README.md
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.
8 changes: 4 additions & 4 deletions cmd/chatgpt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"github.com/kardolus/chatgpt-cli/api/client"
"github.com/kardolus/chatgpt-cli/api/http"
utils2 "github.com/kardolus/chatgpt-cli/cmd/chatgpt/utils"
"github.com/kardolus/chatgpt-cli/cmd/chatgpt/utils"
"github.com/kardolus/chatgpt-cli/internal"
"github.com/spf13/pflag"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -230,7 +230,7 @@ func run(cmd *cobra.Command, args []string) error {
}

if cmd.Flag("prompt").Changed {
prompt, err := utils2.FileToString(promptFile)
prompt, err := utils.FileToString(promptFile)
if err != nil {
return err
}
Expand Down Expand Up @@ -278,7 +278,7 @@ func run(cmd *cobra.Command, args []string) error {
defer rl.Close()

commandPrompt := func(counter, usage int) string {
return utils2.FormatPrompt(c.Config.CommandPrompt, counter, usage, time.Now())
return utils.FormatPrompt(c.Config.CommandPrompt, counter, usage, time.Now())
}

qNum, usage := 1, 0
Expand All @@ -291,7 +291,7 @@ func run(cmd *cobra.Command, args []string) error {
return nil
}

fmtOutputPrompt := utils2.FormatPrompt(c.Config.OutputPrompt, qNum, usage, time.Now())
fmtOutputPrompt := utils.FormatPrompt(c.Config.OutputPrompt, qNum, usage, time.Now())

if queryMode {
result, qUsage, err := c.Query(input)
Expand Down
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions test/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/kardolus/chatgpt-cli/config"
"github.com/kardolus/chatgpt-cli/history"
"github.com/kardolus/chatgpt-cli/internal"
utils2 "github.com/kardolus/chatgpt-cli/test"
"github.com/kardolus/chatgpt-cli/test"
"github.com/onsi/gomega/gexec"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
Expand Down Expand Up @@ -580,7 +580,7 @@ func testIntegration(t *testing.T, when spec.G, it spec.S) {
historyFile := path.Join(filePath, "history", "default.json")
Expect(historyFile).NotTo(BeAnExistingFile())

bytes, err := utils2.FileToBytes("history.json")
bytes, err := test.FileToBytes("history.json")
Expect(err).NotTo(HaveOccurred())

Expect(os.WriteFile(legacyFile, bytes, 0644)).To(Succeed())
Expand Down
4 changes: 2 additions & 2 deletions test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func FileToBytes(fileName string) ([]byte, error) {
err error
)
if strings.Contains(thisFile, "vendor") {
urlPath, err = filepath.Abs(path.Join(thisFile, "../../../../../..", "resources", "testdata", fileName))
urlPath, err = filepath.Abs(path.Join(thisFile, "../../../../../..", "test", "data", fileName))
} else {
urlPath, err = filepath.Abs(path.Join(thisFile, "../..", "resources", "testdata", fileName))
urlPath, err = filepath.Abs(path.Join(thisFile, "../..", "test", "data", fileName))
}

if err != nil {
Expand Down

0 comments on commit 5b4e141

Please sign in to comment.