Skip to content

Commit 2862447

Browse files
authored
[#107] Updated readme.
1 parent ad26e1c commit 2862447

File tree

1 file changed

+142
-79
lines changed

1 file changed

+142
-79
lines changed

genai/README.md

+142-79
Original file line numberDiff line numberDiff line change
@@ -1,169 +1,232 @@
1-
# GenAI Package
1+
# Golly
22

3-
The `genai` package provides functionality for interacting with generative AI models. This package includes support for managing sessions, exchanges, and models, as well as handling memory and templates.
4-
5-
## Installation
6-
7-
To install the package, use the following command:
8-
9-
```sh
10-
go get github.com/nandlabs/golly/genai
11-
```
3+
Golly is a generative AI framework that allows you to create and manage AI models.
124

135
## Index
146

157
- [Installation](#installation)
168
- [Usage](#usage)
17-
- [Creating a Model](#creating-a-model)
18-
- [Creating a Session](#creating-a-session)
19-
- [Adding Exchanges](#adding-exchanges)
20-
- [Contextualizing Queries](#contextualizing-queries)
9+
- [Creating a Provider](#creating-a-provider)
10+
- [Using Templates](#using-templates)
11+
- [Managing Options](#managing-options)
12+
- [Memory Management](#memory-management)
13+
- [Messages](#messages)
2114
- [Components](#components)
22-
- [Model](#model)
23-
- [Session](#session)
24-
- [Exchange](#exchange)
25-
- [Memory](#memory)
15+
- [Provider](#provider)
2616
- [Template](#template)
27-
- [License](#license)
17+
- [Options](#options)
18+
- [Memory](#memory)
19+
- [Message](#message)
20+
21+
## Installation
22+
23+
To install Golly, use the following command:
24+
25+
```sh
26+
go get -u oss.nandlabs.io/golly
27+
```
2828

2929
## Usage
3030

31-
### Creating a Model
31+
### Creating a Provider
3232

33-
To create a model, you need to implement the `Model` interface. Here is an example of a simple model implementation:
33+
To create a new provider, implement the `Provider` interface:
3434

3535
```go
3636
package main
3737

3838
import (
39+
"oss.nandlabs.io/golly/genai"
3940
"fmt"
40-
"github.com/nandlabs/golly/genai"
4141
)
4242

43-
type SimpleModel struct {
44-
genai.AbstractModel
43+
type MyProvider struct {
44+
// ...existing code...
45+
}
46+
47+
func (p *MyProvider) Name() string {
48+
return "MyProvider"
49+
}
50+
51+
func (p *MyProvider) Description() string {
52+
return "A custom provider"
4553
}
4654

47-
func (m *SimpleModel) Generate(exchange genai.Exchange) error {
48-
// Implement the generation logic here
49-
return nil
55+
func (p *MyProvider) Version() string {
56+
return "1.0.0"
57+
}
58+
59+
func (p *MyProvider) Author() string {
60+
return "Author Name"
61+
}
62+
63+
func (p *MyProvider) License() string {
64+
return "MIT"
65+
}
66+
67+
func (p *MyProvider) Supports(model, mime string) (consumer bool, provider bool) {
68+
// ...existing code...
69+
}
70+
71+
func (p *MyProvider) Accepts(model string) []string {
72+
// ...existing code...
73+
}
74+
75+
func (p *MyProvider) Produces(model string) []string {
76+
// ...existing code...
77+
}
78+
79+
func (p *MyProvider) Generate(model string, exchange genai.Exchange, options *genai.Options) error {
80+
// ...existing code...
81+
}
82+
83+
func (p *MyProvider) GenerateStream(model string, exchange genai.Exchange, handler func(reader io.Reader), options genai.Options) error {
84+
// ...existing code...
5085
}
5186

5287
func main() {
53-
model := &SimpleModel{
54-
AbstractModel: genai.AbstractModel{
55-
name: "SimpleModel",
56-
description: "A simple generative AI model",
57-
version: "1.0",
58-
author: "Author Name",
59-
license: "MIT",
60-
},
61-
}
62-
fmt.Println("Model created:", model.Name())
88+
provider := &MyProvider{}
89+
genai.Providers.Register(provider)
90+
fmt.Println("Provider registered:", provider.Name())
6391
}
6492
```
6593

66-
### Creating a Session
94+
### Using Templates
6795

68-
To create a session, you need to use the `LocalSession` struct. Here is an example:
96+
To create and use templates, use the `PromptTemplate` interface and related functions:
6997

7098
```go
7199
package main
72100

73101
import (
102+
"oss.nandlabs.io/golly/genai"
74103
"fmt"
75-
"github.com/nandlabs/golly/genai"
76104
)
77105

78106
func main() {
79-
model := &SimpleModel{
80-
AbstractModel: genai.AbstractModel{
81-
name: "SimpleModel",
82-
description: "A simple generative AI model",
83-
version: "1.0",
84-
author: "Author Name",
85-
license: "MIT",
86-
},
107+
templateContent := "Hello, {{.Name}}!"
108+
templateID := "greeting"
109+
110+
tmpl, err := genai.NewGoTemplate(templateID, templateContent)
111+
if err != nil {
112+
fmt.Println("Error creating template:", err)
113+
return
87114
}
88115

89-
session := &genai.LocalSession{
90-
id: "session1",
91-
model: model,
92-
attributes: make(map[string]interface{}),
93-
memory: genai.NewRamMemory(),
94-
contextualiseTemplate: genai.NewGoTemplate(""),
116+
data := map[string]any{
117+
"Name": "World",
118+
}
119+
120+
result, err := tmpl.FormatAsText(data)
121+
if err != nil {
122+
fmt.Println("Error formatting template:", err)
123+
return
95124
}
96125

97-
fmt.Println("Session created:", session.Id())
126+
fmt.Println(result)
98127
}
99128
```
100129

101-
### Adding Exchanges
130+
### Managing Options
102131

103-
To add exchanges to a session, you can use the `Add` method. Here is an example:
132+
To manage options for the provider, use the `Options` and `OptionsBuilder` structs:
104133

105134
```go
106135
package main
107136

108137
import (
138+
"oss.nandlabs.io/golly/genai"
109139
"fmt"
110-
"github.com/nandlabs/golly/genai"
111140
)
112141

113142
func main() {
114-
// ...existing code...
115-
116-
exchange := genai.NewExchange("exchange1")
117-
message, _ := exchange.AddTxtMsg("Hello, AI!", genai.UserActor)
118-
session.SaveExchange(exchange)
143+
options := genai.NewOptionsBuilder().
144+
SetMaxTokens(100).
145+
SetTemperature(0.7).
146+
Build()
119147

120-
fmt.Println("Exchange added:", message.String())
148+
fmt.Println("Max Tokens:", options.GetMaxTokens(0))
149+
fmt.Println("Temperature:", options.GetTemperature(0))
121150
}
122151
```
123152

124-
### Contextualizing Queries
153+
### Memory Management
125154

126-
To contextualize queries based on previous exchanges, you can use the `Contextualise` method. Here is an example:
155+
To manage memory, use the `Memory` interface and related functions:
127156

128157
```go
129158
package main
130159

131160
import (
161+
"oss.nandlabs.io/golly/genai"
132162
"fmt"
133-
"github.com/nandlabs/golly/genai"
134163
)
135164

136165
func main() {
137-
// ...existing code...
166+
memory := genai.NewRamMemory()
167+
sessionID := "session1"
168+
exchange := genai.Exchange{
169+
// ...existing code...
170+
}
138171

139-
newQuestion, err := session.Contextualise("What is the weather like?", 5)
172+
err := memory.Add(sessionID, exchange)
140173
if err != nil {
141-
fmt.Println("Error contextualizing query:", err)
174+
fmt.Println("Error adding to memory:", err)
142175
return
143176
}
144177

145-
fmt.Println("Contextualized query:", newQuestion)
178+
exchanges, err := memory.Fetch(sessionID, "")
179+
if err != nil {
180+
fmt.Println("Error fetching from memory:", err)
181+
return
182+
}
183+
184+
fmt.Println("Exchanges:", exchanges)
185+
}
186+
```
187+
188+
### Messages
189+
190+
To work with messages, use the `Message` struct:
191+
192+
```go
193+
package main
194+
195+
import (
196+
"oss.nandlabs.io/golly/genai"
197+
"bytes"
198+
"fmt"
199+
)
200+
201+
func main() {
202+
message := &genai.Message{
203+
rwer: bytes.NewBufferString("Hello, World!"),
204+
mimeType: "text/plain",
205+
}
206+
207+
fmt.Println("Message MIME type:", message.Mime())
208+
fmt.Println("Message content:", message.String())
146209
}
147210
```
148211

149212
## Components
150213

151-
### Model
214+
### Provider
152215

153-
The `Model` interface represents a generative AI model. It includes methods for generating responses and handling input and output MIME types.
216+
The `Provider` interface represents a generative AI model. It includes methods for generating responses and handling input and output MIME types.
154217

155-
### Session
218+
### Template
156219

157-
The `Session` interface represents a session with a generative AI model. It includes methods for managing exchanges and contextualizing queries.
220+
The `PromptTemplate` interface represents a template for formatting prompts. The `goTemplate` struct provides an implementation using Go templates.
158221

159-
### Exchange
222+
### Options
160223

161-
The `Exchange` interface represents an exchange between users and the AI. It includes methods for adding and retrieving messages.
224+
The `Options` struct represents the options for the provider. The `OptionsBuilder` struct provides a builder for creating options.
162225

163226
### Memory
164227

165228
The `Memory` interface represents a memory for storing exchanges. The `RamMemory` struct provides an in-memory implementation.
166229

167-
### Template
230+
### Message
168231

169-
The `PromptTemplate` interface represents a template for formatting prompts. The `goTemplate` struct provides an implementation using Go templates.
232+
The `Message` struct represents a message with MIME type and content.

0 commit comments

Comments
 (0)