chatproxy

package module
v0.0.0-...-9084aec Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2023 License: MIT Imports: 18 Imported by: 0

README

Go ReferenceGo Report Card

Chatproxy

This README ghostwritten by the Chat CLI tool backed by ChatGPT4

Chatproxy is a powerful Golang library that simplifies interactions with OpenAI's GPT-4 model, allowing developers to seamlessly integrate GPT-4 into their Go applications for various tasks. It comes with a collection of ready-to-use command-line tools that serve as examples of how to leverage the chatproxy library. Users can customize the API client, output formats, and authentication methods, making it an indispensable tool for Golang enthusiasts working with AI-powered document generation and processing.

Key Features

  • Effortless integration with GPT-4 for your Go applications
  • Simple API client with customizable settings
  • Common functions for handling messages, conversation history, and errors
  • Collection of handy command-line tools: Ask, Card, Commit, Chat and TLDR

Unlock the power of GPT-4 in your Go projects with Chatproxy and take your applications to the next level.

Default Transcript Logging

By default, Chatproxy records transcripts of your interactions with ChatGPT4, providing an invaluable resource for tracking and understanding the data being sent to OpenAI's powerful AI model. This proves advantageous since it ensures transparency and allows you to be well aware of the information being exchanged.

What is recorded?

Chatproxy logs various types of messages and interactions, including:

  1. User input
  2. Assistant responses (generated by ChatGPT4)
  3. System prompts, instructions, and status updates
Where is the transcript stored?

By default, the logged data is recorded in the transcript field of the ChatGPTClient struct. This holds a comprehensive log of interactions in the form of user inputs, bot responses, and system messages, offering an accessible, all-in-one record.

Benefits of Default Transcript Logging

Default transcript logging in Chatproxy serves multiple purposes:

  • Ensures visibility and transparency when working with OpenAI services
  • Provides insights into the data exchanged with ChatGPT4 to help you understand your usage better
  • Assists in detecting issues, debugging, and optimizing interactions with ChatGPT4 for the desired output
  • Helps you maintain compliance with any data retention or privacy policies, enabling precise control over the information sent to OpenAI

Embrace the convenience and peace of mind offered by Chatproxy's default transcript logging, taking full advantage of data awareness and transparency for your Golang applications using OpenAI and ChatGPT4.

Chatproxy Library

Installation and Usage
go get -u github.com/mr-joshcrane/chatproxy
package main

import (
	"fmt"
	"github.com/mr-joshcrane/chatproxy"
)

func main() {
	answer, err := chatproxy.Ask("What is the capital of France?")
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	fmt.Println("Answer:", answer)
}
Answer: Paris

Ask CLI Tool

Installation and Usage
go install github.com/mr-joshcrane/chatproxy/cmd/ask@latest
ask "What is the capital of France?"
The capital of France is Paris.

Cards CLI Tool

Installation and Usage
go install github.com/mr-joshcrane/chatproxy/cmd/cards@latest
cards path/to/your/file/README.md

Question: What are special commands in the Chat CLI Tool?
Answer: Three special commands: 
  1. ">filename.txt" to load a file,
  2. "<filename.txt" to write response to a file,
  3. "?" to generate comprehension questions.

Commit CLI Tool

Installation and Usage
go install github.com/mr-joshcrane/chatproxy/cmd/commit@latest
commit
Accept Generated Message? (Y)es/(N)o
Add installation and usage instructions for Chatproxy library and CLI tools

Chat CLI Tool

Installation and Usage
go install github.com/mr-joshcrane/chatproxy/cmd/chat@latest
chat
    SYSTEM) Please describe the purpose of this assistant.
    USER) You help me write Golang documentation.
    USER) >chatproxy
    USER) What special commands are available in the Chat CLI tool and what do they
    do?
    USER) What special commands are availble in the chat CLI tool and what do they do?
    ASSISTANT) In the chat CLI tool, there are THREE special commands:
        1. `>filename.txt`: This command is used to load the contents of a local file into the chat session.
        The text in the file will be used as input for the assistant, and it will base its response on that content.
        To use this command, simply type `>` followed by the name of the file you want to load. For example:

   >file.txt

        2. `<filename.txt`: This command is used to write the assistant's response to a local file.
        You can use this command to save the generated content for future reference.
        To use this command, type `<` followed by the name of the file you want to save the response to,
        and then enter your input prompt. For example:

   <output.txt What is the capital of France?

        3.  The `?` command in the chat CLI tool is used to generate comprehension questions based on the provided text.
        It uses Bloom's Taxonomy (2001) to create questions that help assess the understanding of the given content.
        To use this command, simply type `?` at the beginning of the chat input
        and questions will be generated from the content of the current
        conversation. To make sure you were really paying attention!

These special commands help users extend the interactivity between the chat CLI tool and external files, making it more convenient to use different sources of information or store assistant responses for later use.

TLDR CLI Tool

Installation and Usage
go install github.com/mr-joshcrane/chatproxy/cmd/tldr@latest
tldr path/to/your/file.txt
A brief summary of your file.

tldr https://example.site.com
A brief summary of your website.

OPENAI_API_KEY Environment Variable

Purpose: The OPENAI_API_KEY is used to authenticate and authorize API access to OpenAI's GPT-4 services.

Usage: Store the token as an environment variable (OPENAI_API_KEY="YOUR_TOKEN") in your system or application, so that the library can access it automatically.

Obtaining a token: You can get an API key by creating an account on OpenAI's platform at https://beta.openai.com/signup/. After signing up, visit the API Keys section in your account to obtain a token.

User responsibilities: It is crucial to keep the token secret and secure, as it allows access to your OpenAI account and its services. Make sure not to share the token in public repositories or with unauthorized individuals. Additionally, be aware of usage limits and costs associated with OpenAI API services, as you will be billed according to your account's pricing plan.

Always follow OpenAI's guidelines, terms, and conditions when using its services.

Documentation

Index

Constants

View Source
const (
	RoleUser   = "user"
	RoleBot    = "assistant"
	RoleSystem = "system"
)

Role constants that represent the role of the message sender

View Source
const (
	QuestionPrompt = `` /* 657-byte string literal not displayed */

)

Variables

View Source
var NewChatGPTClient = DefaultGPTClient

Functions

func Ask

func Ask(args []string) int

Ask sends a question to the GPT-4 API, aiming to receive a relevant and informed answer. It facilitates user interaction with GPT-4 for knowledge retrieval or problem-solving.

func BotField

func BotField(args []string) int

func Card

func Card(args []string) int

Card generates a set of flashcards from a given file or URL, aiming to enhance learning by summarizing important concepts. It uses GPT-4 for extracting key information in a compact and easy-to-review format.

func Chat

func Chat() int

Chat function initiates the chat with the user and enables interaction between user and the chat proxy. It orchestrates the entire conversational experience with the purpose of assisting the user in various tasks.

func Commit

func Commit() int

Commit analyzes staged Git files, parsing the diff, and generates a meaningful commit message. It aims to streamline the process of creating accurate and informative commit descriptions for better version control.

func CreateAuditLog

func CreateAuditLog() (*os.File, error)

CreateAuditLog creates a new file for recording the conversation's audit log with a timestamped filename. This function helps ensure conversation logs are saved and timestamped, allowing the users to review their chat history later.

func MessageFromFile

func MessageFromFile(path string) (message string, tokenLen int, err error)

MessageFromFile reads the contents of a file, and returns a formatted message with the file name and content, as well as an estimation of the token count. This function enables the bot to include file contents when sending messages to the user.

func MessageToFile

func MessageToFile(content string, path string) error

MessageToFile writes the given content string to a file with the specified path. This function enables the bot to save conversation logs in a file or write user-generated content to a file.

func TLDR

func TLDR(args []string) int

TLDR generates a concise summary of content from a file or URL, aiming to condense important information. It utilizes GPT-4 to help users quickly grasp the key points of large texts.

Types

type ChatGPTClient

type ChatGPTClient struct {
	// contains filtered or unexported fields
}

ChatGPTClient manages interactions with a GPT-based chatbot, providing a way to organize the conversation, handle input/output, and maintain an audit trail.

func DefaultGPTClient

func DefaultGPTClient(opts ...ClientOption) (*ChatGPTClient, error)

NewChatGPTClient initializes the ChatGPTClient with the desired options, allowing customization through functional options so the client can be tailored to specific needs or requirements.

func (*ChatGPTClient) Ask

func (c *ChatGPTClient) Ask(question string) (answer string, err error)

Ask sends a user question to the GPT-4 API, and expects an informed response. This method is part of the ChatGPTClient and allows users to leverage the GPT-4 model for answering queries.

func (*ChatGPTClient) Card

func (c *ChatGPTClient) Card(path string) (cards []string, err error)

Card creates flashcards using the content from a given file or URL. This method, part of the ChatGPTClient, uses the GPT-4 API to break down and condense information into manageable flashcards.

func (*ChatGPTClient) Chat

func (c *ChatGPTClient) Chat()

Chat method handles the conversational flow for the ChatGPTClient, aiming to provide a seamless user experience by managing prompts and strategies.

func (*ChatGPTClient) Chunk

func (c *ChatGPTClient) Chunk(contents io.Reader, chunkSize int) []string

func (*ChatGPTClient) Commit

func (c *ChatGPTClient) Commit() (summary string, err error)

Commit parses the diff of staged Git files and generates an appropriate commit message. This method, part of the ChatGPTClient, helps users maintain clear commit history and conveys changes in a concise and descriptive manner.

func (*ChatGPTClient) CreateEmbeddings

func (c *ChatGPTClient) CreateEmbeddings(origin string, contents io.Reader)

func (*ChatGPTClient) Fprint

func (c *ChatGPTClient) Fprint(a ...interface{})

func (*ChatGPTClient) GetCompletion

func (c *ChatGPTClient) GetCompletion(opts ...CompletionOption) (string, error)

GetCompletion retrieves a response from the chatbot based on the conversation history and any additional options applied.

func (*ChatGPTClient) GetContent

func (c *ChatGPTClient) GetContent(path string) (msg string, err error)

GetContent takes a path, checks if it is a file or URL, and returns the contents of the file or the text of the URL.

func (*ChatGPTClient) GetStrategy

func (c *ChatGPTClient) GetStrategy(input string) Strategy

GetStrategy method selects the appropriate strategy based on the user input, ensuring the correct action is taken to achieve the user's desired outcome.

func (*ChatGPTClient) Log

func (c *ChatGPTClient) Log(role string, message string)

Log logs a chat message with the given role and message. It helps maintain a comprehensive log of interactions in the ChatGPTClient. The primary purpose of this function is to clearly show which role (e.g., user, bot, system) is responsible for a particular message in the conversation.

func (*ChatGPTClient) LogErr

func (c *ChatGPTClient) LogErr(err error)

LogErr logs errors in the ChatGPTClient's errorStream. This makes it possible to capture and handle errors in a standardized manner, enabling efficient debugging and error handling.

func (*ChatGPTClient) LogOut

func (c *ChatGPTClient) LogOut(message ...any)

LogOut logs a message to the ChatGPTClient's output stream. This is useful for logging messages that are not part of the conversation, such as instructions or system status updates.

func (*ChatGPTClient) MessageFromFiles

func (c *ChatGPTClient) MessageFromFiles(path string) (string, error)

MessageFromFiles reads the contents of multiple files in a directory, and returns a combined formatted message with file names and contents. This function allows the bot to send messages with content from multiple files at once to the user without making multiple calls.

func (*ChatGPTClient) Prompt

func (c *ChatGPTClient) Prompt(prompts ...string)

Prompt formats and prints system prompts to the output. It uses yellow color to differentiate system messages from user and bot messages for better visibility. The main purpose of this function is to guide user interactions and ensure clear communication of instructions or system status updates.

func (*ChatGPTClient) RecordMessage

func (c *ChatGPTClient) RecordMessage(role string, message string)

RecordMessage adds a new message in the conversation context, allowing the chatbot to maintain a conversation context. The role parameter provides a mechanism for inserting bot or system responses in addition to user messages.

func (*ChatGPTClient) Relevant

func (c *ChatGPTClient) Relevant(query string) (Similarities, error)

func (*ChatGPTClient) RollbackLastMessage

func (c *ChatGPTClient) RollbackLastMessage() []ChatMessage

RollbackLastMessage serves as an undo functionality, removing the last message from the conversation, and providing a way to recover from erroneous input or chatbot responses.

func (*ChatGPTClient) SetPurpose

func (c *ChatGPTClient) SetPurpose(prompt string)

SetPurpose defines the purpose of the conversation, providing contextual guidance for the chatbot to follow, and aligning the conversation towards a specific topic or goal.

func (*ChatGPTClient) TLDR

func (c *ChatGPTClient) TLDR(path string) (summary string, err error)

TLDR generates a brief summary of the content from a file or URL. This method is part of the ChatGPTClient and leverages the GPT-4 API to present an abstract of the main text, providing a quick overview.

func (*ChatGPTClient) TranscriptPath

func (c *ChatGPTClient) TranscriptPath() string

func (*ChatGPTClient) Vectorize

func (c *ChatGPTClient) Vectorize(origin string, s []string) ([]Embedding, error)

type ChatMessage

type ChatMessage struct {
	Content string
	Role    string
}

ChatMessage represents a message in the chat, providing context and a way to model conversation between different participant roles (e.g., user, bot, system).

type ClientOption

type ClientOption func(*ChatGPTClient) *ChatGPTClient

ClientOption is used to flexibly configure the ChatGPTClient to meet various requirements and use cases, such as custom input/output handling or error reporting.

func WithFixedResponse

func WithFixedResponse(response string) ClientOption

WithFixedResponse configures the ChatGPTClient to return a predetermined response, offering quicker or consistent replies, or simulating specific behavior for test cases.

func WithInput

func WithInput(input io.Reader) ClientOption

WithInput assigns a custom input reader for ChatGPTClient, allowing the client to read input from any source, offering improved flexibility and adaptability.

func WithOutput

func WithOutput(output, err io.Writer) ClientOption

WithOutput allows customizing the output/error handling in the ChatGPTClient, making the client more adaptable to different environments or reporting workflows.

func WithStreaming

func WithStreaming(streaming bool) ClientOption

WithStreaming controls the streaming mode of the ChatGPTClient, giving the user the choice between streamed responses for real-time interactions or buffered responses for complete replies.

func WithToken

func WithToken(token string) ClientOption

WithToken uses the provided token for authentication when creating a new ChatGPTClient.

func WithTranscript

func WithTranscript(audit io.Writer) ClientOption

WithTranscript enables keeping a log of all conversation messages, ensuring a persistent record that can be useful for auditing, debugging, or further analysis.

type CompletionOption

type CompletionOption func(*openai.ChatCompletionRequest) *openai.ChatCompletionRequest

CompletionOption is used to customize the behavior of the openai.ChatCompletionRequest to suit different use cases, such as setting stop words or modifying token limits.

func WithFixedResponseAPIValidate

func WithFixedResponseAPIValidate(response string) CompletionOption

WithFixedResponseAPIValidate still makes an API call (ensuring request and token length validation) but enforces a specific response from the chatbot, ensuring a known output and avoiding unpredictable or unnecessary responses during validation.

type Default

type Default struct {
	// contains filtered or unexported fields
}

func (Default) Execute

func (s Default) Execute(c *ChatGPTClient) error

Execute method for Default strategy is responsible for managing a typical chat interaction by sending user input to the OpenAI API and receiving a response.

type Embedding

type Embedding struct {
	Origin         string
	OriginSequence int
	PlainText      string
	Vector         []float64
}

type Exit

type Exit struct{}

func (Exit) Execute

func (s Exit) Execute(c *ChatGPTClient) error

Execute method for Exit strategy gracefully manages the termination of the chat session when the user decides to exit.

type FileLoad

type FileLoad struct {
	// contains filtered or unexported fields
}

func (FileLoad) Execute

func (s FileLoad) Execute(c *ChatGPTClient) error

Execute method for FileLoad strategy handles loading file contents to be processed by ChatGPTClient, which enables users to provide input via files instead of just through the chat interface.

type FileWrite

type FileWrite struct {
	// contains filtered or unexported fields
}

func (FileWrite) Execute

func (s FileWrite) Execute(c *ChatGPTClient) error

Execute method for FileWrite strategy allows writing output from the chat interaction to a file, offering an organized and convenient way to store results.

type Similarities

type Similarities struct {
	Query           string
	RelevantVectors []Similarity
}

func (Similarities) Top

func (s Similarities) Top(n int) []string

type Similarity

type Similarity struct {
	PlainText string
	Score     float64
}

type Strategy

type Strategy interface {
	Execute(*ChatGPTClient) error
}

Directories

Path Synopsis
cmd
ask

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL