Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aratan/ollama4go
ollama-chat (Module for go)
https://github.com/aratan/ollama4go
golang modeule ollama
Last synced: 11 days ago
JSON representation
ollama-chat (Module for go)
- Host: GitHub
- URL: https://github.com/aratan/ollama4go
- Owner: aratan
- License: gpl-3.0
- Created: 2024-08-13T09:24:07.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-08-13T10:47:13.000Z (3 months ago)
- Last Synced: 2024-10-29T23:30:52.488Z (17 days ago)
- Topics: golang, modeule, ollama
- Language: Go
- Homepage: https://github.com/aratan
- Size: 28.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ollama4go
`ollama4go` es un paquete de Go que facilita la interacción con la API del chat en Ollama.
Cuando hagas tu app tendras que instalar el paquete así:is a Go package that facilitates interaction with the Ollama chat API. When you make your app you will need to install the package like this:
```go
go get github.com/aratan/ollama4go
```### Main.go
```go
package mainimport (
"fmt"
"github.com/aratan/ollama4go"
)func main() {
client := llamachat.NewChatClient("http://localhost:11434/api/chat")
request := llamachat.ChatRequest{
Model: "llama3.1",
Messages: []llamachat.Message{
{Role: "user", Content: "Hello!"},
},
Options: llamachat.Options{
Seed: 101,
Temperature: 0,
},
}response, err := client.SendChatRequest(request)
if err != nil {
fmt.Println("Error:", err)
return
}fmt.Println("Respuesta del asistente:", response)
}
```# Otra opcion
### config.yaml
```yaml
api:
url: "http://localhost:11434/api/chat"chat:
model: "llama3.1"
seed: 101
temperature: 0message:
role: "user"
content: "en python como seria el codigo: Hello!"
``````go
package mainimport (
"fmt"
"io/ioutil"
"log""github.com/aratan/ollama4go"
"gopkg.in/yaml.v2"
)type Config struct {
API struct {
URL string `yaml:"url"`
} `yaml:"api"`
Chat struct {
Model string `yaml:"model"`
Seed int `yaml:"seed"`
Temperature float32 `yaml:"temperature"`
} `yaml:"chat"`
Message struct {
Role string `yaml:"role"`
Content string `yaml:"content"`
} `yaml:"message"`
}func loadConfig() (*Config, error) {
var config Config
data, err := ioutil.ReadFile("config.yaml")
if err != nil {
return nil, err
}
err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, err
}
return &config, nil
}func main() {
config, err := loadConfig()
if err != nil {
log.Fatalf("error: %v", err)
}client := llamachat.NewChatClient(config.API.URL)
request := llamachat.ChatRequest{
Model: config.Chat.Model,
Messages: []llamachat.Message{
{Role: config.Message.Role, Content: config.Message.Content},
},
Options: llamachat.Options{
Seed: config.Chat.Seed,
Temperature: float64(config.Chat.Temperature), // Conversión explícita
},
}response, err := client.SendChatRequest(request)
if err != nil {
fmt.Println("Error:", err)
return
}fmt.Println("*** ``` ", response, "``` ***")
}```
## Otra forma```go
package mainimport (
"fmt"
"io/ioutil"
"log"
"os""github.com/aratan/ollama4go"
"gopkg.in/yaml.v2"
)type Config struct {
API struct {
URL string `yaml:"url"`
} `yaml:"api"`
Chat struct {
Model string `yaml:"model"`
Seed int `yaml:"seed"`
Temperature float32 `yaml:"temperature"`
} `yaml:"chat"`
Message struct {
Role string `yaml:"role"`
Content string
} `yaml:"message"`
}func loadConfig() (*Config, error) {
var config Config
data, err := ioutil.ReadFile("config.yaml")
if err != nil {
return nil, err
}
err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, err
}
return &config, nil
}func main() {
if len(os.Args) < 2 {
log.Fatalf("Error: se requiere un argumento para 'Content'")
}content := os.Args[1]
config, err := loadConfig()
if err != nil {
log.Fatalf("error: %v", err)
}client := llamachat.NewChatClient(config.API.URL)
request := llamachat.ChatRequest{
Model: config.Chat.Model,
Messages: []llamachat.Message{
{Role: config.Message.Role, Content: content},
},
Options: llamachat.Options{
Seed: config.Chat.Seed,
Temperature: float64(config.Chat.Temperature), // Conversión explícita
},
}response, err := client.SendChatRequest(request)
if err != nil {
fmt.Println("Error:", err)
return
}fmt.Println("Lexy: ", response)
}
```