|
1 |
| -# GenAI Package |
| 1 | +# Golly |
2 | 2 |
|
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. |
12 | 4 |
|
13 | 5 | ## Index
|
14 | 6 |
|
15 | 7 | - [Installation](#installation)
|
16 | 8 | - [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) |
21 | 14 | - [Components](#components)
|
22 |
| - - [Model](#model) |
23 |
| - - [Session](#session) |
24 |
| - - [Exchange](#exchange) |
25 |
| - - [Memory](#memory) |
| 15 | + - [Provider](#provider) |
26 | 16 | - [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 | +``` |
28 | 28 |
|
29 | 29 | ## Usage
|
30 | 30 |
|
31 |
| -### Creating a Model |
| 31 | +### Creating a Provider |
32 | 32 |
|
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: |
34 | 34 |
|
35 | 35 | ```go
|
36 | 36 | package main
|
37 | 37 |
|
38 | 38 | import (
|
| 39 | + "oss.nandlabs.io/golly/genai" |
39 | 40 | "fmt"
|
40 |
| - "github.com/nandlabs/golly/genai" |
41 | 41 | )
|
42 | 42 |
|
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" |
45 | 53 | }
|
46 | 54 |
|
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... |
50 | 85 | }
|
51 | 86 |
|
52 | 87 | 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()) |
63 | 91 | }
|
64 | 92 | ```
|
65 | 93 |
|
66 |
| -### Creating a Session |
| 94 | +### Using Templates |
67 | 95 |
|
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: |
69 | 97 |
|
70 | 98 | ```go
|
71 | 99 | package main
|
72 | 100 |
|
73 | 101 | import (
|
| 102 | + "oss.nandlabs.io/golly/genai" |
74 | 103 | "fmt"
|
75 |
| - "github.com/nandlabs/golly/genai" |
76 | 104 | )
|
77 | 105 |
|
78 | 106 | 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 |
87 | 114 | }
|
88 | 115 |
|
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 |
95 | 124 | }
|
96 | 125 |
|
97 |
| - fmt.Println("Session created:", session.Id()) |
| 126 | + fmt.Println(result) |
98 | 127 | }
|
99 | 128 | ```
|
100 | 129 |
|
101 |
| -### Adding Exchanges |
| 130 | +### Managing Options |
102 | 131 |
|
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: |
104 | 133 |
|
105 | 134 | ```go
|
106 | 135 | package main
|
107 | 136 |
|
108 | 137 | import (
|
| 138 | + "oss.nandlabs.io/golly/genai" |
109 | 139 | "fmt"
|
110 |
| - "github.com/nandlabs/golly/genai" |
111 | 140 | )
|
112 | 141 |
|
113 | 142 | 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() |
119 | 147 |
|
120 |
| - fmt.Println("Exchange added:", message.String()) |
| 148 | + fmt.Println("Max Tokens:", options.GetMaxTokens(0)) |
| 149 | + fmt.Println("Temperature:", options.GetTemperature(0)) |
121 | 150 | }
|
122 | 151 | ```
|
123 | 152 |
|
124 |
| -### Contextualizing Queries |
| 153 | +### Memory Management |
125 | 154 |
|
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: |
127 | 156 |
|
128 | 157 | ```go
|
129 | 158 | package main
|
130 | 159 |
|
131 | 160 | import (
|
| 161 | + "oss.nandlabs.io/golly/genai" |
132 | 162 | "fmt"
|
133 |
| - "github.com/nandlabs/golly/genai" |
134 | 163 | )
|
135 | 164 |
|
136 | 165 | func main() {
|
137 |
| - // ...existing code... |
| 166 | + memory := genai.NewRamMemory() |
| 167 | + sessionID := "session1" |
| 168 | + exchange := genai.Exchange{ |
| 169 | + // ...existing code... |
| 170 | + } |
138 | 171 |
|
139 |
| - newQuestion, err := session.Contextualise("What is the weather like?", 5) |
| 172 | + err := memory.Add(sessionID, exchange) |
140 | 173 | if err != nil {
|
141 |
| - fmt.Println("Error contextualizing query:", err) |
| 174 | + fmt.Println("Error adding to memory:", err) |
142 | 175 | return
|
143 | 176 | }
|
144 | 177 |
|
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()) |
146 | 209 | }
|
147 | 210 | ```
|
148 | 211 |
|
149 | 212 | ## Components
|
150 | 213 |
|
151 |
| -### Model |
| 214 | +### Provider |
152 | 215 |
|
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. |
154 | 217 |
|
155 |
| -### Session |
| 218 | +### Template |
156 | 219 |
|
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. |
158 | 221 |
|
159 |
| -### Exchange |
| 222 | +### Options |
160 | 223 |
|
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. |
162 | 225 |
|
163 | 226 | ### Memory
|
164 | 227 |
|
165 | 228 | The `Memory` interface represents a memory for storing exchanges. The `RamMemory` struct provides an in-memory implementation.
|
166 | 229 |
|
167 |
| -### Template |
| 230 | +### Message |
168 | 231 |
|
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