Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
seefs001 committed Oct 15, 2024
1 parent 5307511 commit f6a24bb
Show file tree
Hide file tree
Showing 21 changed files with 1,762 additions and 331 deletions.
181 changes: 180 additions & 1 deletion x/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -796,10 +796,189 @@ if err != nil {
fmt.Println(encoded) // Output: https://example.com/path?q=%E4%BD%A0%E5%A5%BD
```

#### StringToBool

```go
func StringToBool(s string) bool
```

Converts a string to a boolean value.

Example:
```go
fmt.Println(x.StringToBool("true")) // Output: true
fmt.Println(x.StringToBool("1")) // Output: true
fmt.Println(x.StringToBool("false")) // Output: false
fmt.Println(x.StringToBool("0")) // Output: false
```

#### StringToInt

```go
func StringToInt(s string) (int, error)
```

Converts a string to an integer.

Example:
```go
num, err := x.StringToInt("42")
if err != nil {
// handle error
}
fmt.Println(num) // Output: 42
```

#### StringToInt64

```go
func StringToInt64(s string) (int64, error)
```

Converts a string to an int64.

Example:
```go
num, err := x.StringToInt64("9223372036854775807")
if err != nil {
// handle error
}
fmt.Println(num) // Output: 9223372036854775807
```

#### StringToUint

```go
func StringToUint(s string) (uint, error)
```

Converts a string to an unsigned integer.

Example:
```go
num, err := x.StringToUint("42")
if err != nil {
// handle error
}
fmt.Println(num) // Output: 42
```

#### StringToUint64

```go
func StringToUint64(s string) (uint64, error)
```

Converts a string to a uint64.

Example:
```go
num, err := x.StringToUint64("18446744073709551615")
if err != nil {
// handle error
}
fmt.Println(num) // Output: 18446744073709551615
```

#### StringToFloat64

```go
func StringToFloat64(s string) (float64, error)
```

Converts a string to a float64.

Example:
```go
num, err := x.StringToFloat64("3.14159")
if err != nil {
// handle error
}
fmt.Println(num) // Output: 3.14159
```

#### StringToDuration

```go
func StringToDuration(s string) (time.Duration, error)
```

Converts a string to a time.Duration.

Example:
```go
duration, err := x.StringToDuration("5m30s")
if err != nil {
// handle error
}
fmt.Println(duration) // Output: 5m30s
```

#### StringToMap

```go
func StringToMap(s, pairSep, kvSep string) map[string]string
```

Converts a string to a map[string]string.

Example:
```go
str := "key1=value1,key2=value2"
m := x.StringToMap(str, ",", "=")
fmt.Println(m) // Output: map[key1:value1 key2:value2]
```

#### BindData

```go
func BindData(v interface{}, data map[string][]string) error
```

Binds data from a map to a struct based on tags.

Example:
```go
type User struct {
Name string `form:"name"`
Age int `form:"age"`
}

data := map[string][]string{
"name": {"John Doe"},
"age": {"30"},
}

var user User
err := x.BindData(&user, data)
if err != nil {
// handle error
}
fmt.Printf("%+v\n", user) // Output: {Name:John Doe Age:30}
```

#### JSONToURLValues

```go
func JSONToURLValues(jsonStr string) (url.Values, error)
```

Converts a JSON string to url.Values.

Example:
```go
jsonStr := `{"key1": "value1", "key2": ["value2", "value3"]}`
values, err := x.JSONToURLValues(jsonStr)
if err != nil {
// handle error
}
fmt.Println(values.Encode()) // Output: key1=value1&key2=value2&key2=value3
```

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
136 changes: 132 additions & 4 deletions xai/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
# XAI Package

The XAI package provides a robust client for interacting with OpenAI's API, including support for text generation, embeddings, and image generation. It also includes integration with Midjourney for advanced image generation capabilities.
The XAI package provides a robust client for interacting with OpenAI's API, including support for text generation, embeddings, and image generation. It also includes integration with Midjourney for advanced image generation capabilities and an Agent system for complex interactions.

## Table of Contents
- [XAI Package](#xai-package)
- [Table of Contents](#table-of-contents)
- [Installation](#installation)
- [Basic Usage](#basic-usage)
- [Creating a Client](#creating-a-client)
- [Text Generation](#text-generation)
- [Streaming Text Generation](#streaming-text-generation)
- [Quick Text Generation](#quick-text-generation)
- [Creating Embeddings](#creating-embeddings)
- [Image Generation](#image-generation)
- [Midjourney Integration](#midjourney-integration)
- [Advanced Usage](#advanced-usage)
- [Custom HTTP Client](#custom-http-client)
- [Environment Variables](#environment-variables)
- [Agents](#agents)
- [Collaborative Agents](#collaborative-agents)
- [Streaming Agent Interactions](#streaming-agent-interactions)
- [Error Handling](#error-handling)
- [OpenAI Client Details](#openai-client-details)
- [Structures](#structures)
- [Key Functions](#key-functions)
- [Chat Completions](#chat-completions)
- [Completions](#completions)
- [Image Generation](#image-generation-1)
- [Embeddings](#embeddings)
- [Audio](#audio)
- [Configuration Options](#configuration-options)
- [Contributing](#contributing)
- [License](#license)

## Installation

```go
import "github.com/seefspkg/xai"
```

## Usage
## Basic Usage

### Creating a Client

Expand Down Expand Up @@ -122,9 +153,46 @@ The package supports loading API key and base URL from environment variables:
- `OPENAI_API_KEY`: Your OpenAI API key
- `OPENAI_API_BASE`: Base URL for the OpenAI API

## Error Handling
### Agents

```go
agent := xai.NewAgent(client, toolExecutor,
xai.WithAgentModel("gpt-4"),
xai.WithAgentSystemPrompt(xai.ExpertSystemPrompt),
xai.WithAgentMaxIterations(5),
xai.WithAgentTemperature(0.7),
xai.WithAgentTools(tools),
xai.WithAgentName("ExpertAgent"),
xai.WithAgentDebug(true),
xai.WithAgentCallback(callbackFunc),
xai.WithAgentMemory(xai.NewSimpleMemory(10)),
)

result, err := agent.Run(ctx, "Analyze the current economic trends")
```

### Collaborative Agents

```go
result, err := agent.CollaborateWithAgents(ctx, "Develop a marketing strategy", map[string]*xai.Agent{
"MarketResearch": marketResearchAgent,
"ContentCreation": contentCreationAgent,
"Analytics": analyticsAgent,
})
```

All methods return errors when appropriate. Always check and handle these errors in your code.
### Streaming Agent Interactions

```go
eventChan, result, err := agent.RunWithEvents(ctx, "Explain quantum computing")

for event := range eventChan {
// Process events (thoughts, actions, observations, etc.)
fmt.Printf("Event: %s, Data: %v\n", event.Type, event.Data)
}
```

## Error Handling

```go
text, err := client.GenerateText(ctx, options)
Expand All @@ -133,8 +201,68 @@ if err != nil {
}
```

## OpenAI Client Details

### Structures

- `OpenAIClient`: The main client struct for interacting with OpenAI API.
- `ChatCompletionMessage`: Represents a message in the chat completion.
- `Tool`: Represents a tool that can be used in chat completions.
- `Function`: Describes a function that can be called by the model.

### Key Functions

#### Chat Completions
```go
func (c *OpenAIClient) CreateChatCompletion(ctx context.Context, req CreateChatCompletionRequest) (*CreateChatCompletionResponse, error)
func (c *OpenAIClient) CreateChatCompletionStream(ctx context.Context, req CreateChatCompletionRequest) (<-chan ChatCompletionChunk, error)
```

#### Completions
```go
func (c *OpenAIClient) CreateCompletion(ctx context.Context, req CreateCompletionRequest) (*CreateCompletionResponse, error)
```

#### Image Generation
```go
func (c *OpenAIClient) CreateImage(ctx context.Context, req CreateImageRequest) (*ImagesResponse, error)
func (c *OpenAIClient) CreateImageEdit(ctx context.Context, req CreateImageEditRequest) (*ImagesResponse, error)
func (c *OpenAIClient) CreateImageVariation(ctx context.Context, req CreateImageVariationRequest) (*ImagesResponse, error)
```

#### Embeddings
```go
func (c *OpenAIClient) CreateEmbedding(ctx context.Context, req CreateEmbeddingRequest) (*CreateEmbeddingResponse, error)
```

#### Audio
```go
func (c *OpenAIClient) CreateSpeech(ctx context.Context, req CreateSpeechRequest) ([]byte, error)
func (c *OpenAIClient) CreateTranscription(ctx context.Context, req CreateTranscriptionRequest) (interface{}, error)
```

### Configuration Options

- `WithBaseURL`: Sets the base URL for the OpenAI API.
- `WithAPIKey`: Sets the API key for authentication.
- `WithHTTPClient`: Sets a custom HTTP client.
- `WithModel`: Sets the default model for the OpenAI client.
- `WithDebug`: Enables or disables debug mode.

Example:
```go
client := NewOpenAIClient(
WithBaseURL("https://api.openai.com"),
WithAPIKey("your-api-key"),
WithModel("gpt-3.5-turbo"),
WithDebug(true),
)
```

## Contributing

Contributions are welcome! Please read our contributing guidelines and submit pull requests to our repository.

## License

This project is licensed under the MIT License - see the LICENSE file for details.
Loading

0 comments on commit f6a24bb

Please sign in to comment.