init
This commit is contained in:
commit
234288da9c
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1 @@
|
||||||
|
package llm
|
|
@ -0,0 +1,31 @@
|
||||||
|
package llm
|
||||||
|
|
||||||
|
// Memory is a mock implementation of EmbedderCompleter
|
||||||
|
type Memory struct {
|
||||||
|
EmbedReturns [][]float32
|
||||||
|
EmbedError error
|
||||||
|
CompleteReturns []string
|
||||||
|
CompleteError error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Memory) Embed(input string) ([]float32, error) {
|
||||||
|
if m.EmbedError != nil {
|
||||||
|
return nil, m.EmbedError
|
||||||
|
}
|
||||||
|
res := m.EmbedReturns[0]
|
||||||
|
if len(m.EmbedReturns) > 1 {
|
||||||
|
m.EmbedReturns = m.EmbedReturns[1:]
|
||||||
|
}
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Memory) Complete(input string) (string, error) {
|
||||||
|
if m.CompleteError != nil {
|
||||||
|
return "", m.CompleteError
|
||||||
|
}
|
||||||
|
res := m.CompleteReturns[0]
|
||||||
|
if len(m.CompleteReturns) > 1 {
|
||||||
|
m.CompleteReturns = m.CompleteReturns[1:]
|
||||||
|
}
|
||||||
|
return res, nil
|
||||||
|
}
|
|
@ -0,0 +1,127 @@
|
||||||
|
package llm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CompletionRequest struct {
|
||||||
|
System string `json:"system"`
|
||||||
|
Prompt string `json:"prompt"`
|
||||||
|
Model string `json:"model"`
|
||||||
|
Streaming bool `json:"stream"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CompletionResponse struct {
|
||||||
|
Response string `json:"response"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Ollama struct {
|
||||||
|
baseURL string
|
||||||
|
embedModel string
|
||||||
|
completeModel string
|
||||||
|
client *http.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewOllama(baseURL, embedModel, completeModel string) *Ollama {
|
||||||
|
return &Ollama{
|
||||||
|
baseURL: baseURL,
|
||||||
|
embedModel: embedModel,
|
||||||
|
completeModel: completeModel,
|
||||||
|
client: &http.Client{Timeout: 600 * time.Second},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Ollama) Complete(system, prompt string) (string, error) {
|
||||||
|
url := fmt.Sprintf("%s/api/generate", o.baseURL)
|
||||||
|
requestBody := CompletionRequest{
|
||||||
|
Prompt: prompt,
|
||||||
|
Model: o.completeModel,
|
||||||
|
System: system,
|
||||||
|
}
|
||||||
|
jsonData, err := json.Marshal(requestBody)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("could not marshal request to json: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("could not post request to ollama: %v", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
|
return "", fmt.Errorf("received non-successful status code: %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("could not read response: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var completionResponse CompletionResponse
|
||||||
|
err = json.Unmarshal(body, &completionResponse)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("could not unmarshal response: %v ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return completionResponse.Response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Ollama) Embed(inputText string) ([]float32, error) {
|
||||||
|
reqBody := map[string]interface{}{
|
||||||
|
"model": "text-embedding-3-small",
|
||||||
|
"input": inputText,
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonBody, err := json.Marshal(reqBody)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error marshaling request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(
|
||||||
|
context.Background(),
|
||||||
|
"POST",
|
||||||
|
o.baseURL+"/v1/embeddings",
|
||||||
|
strings.NewReader(string(jsonBody)),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error creating request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
// req.Header.Set("Authorization", "Bearer "+o.apiKey)
|
||||||
|
|
||||||
|
resp, err := o.client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error making request: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
body, _ := io.ReadAll(resp.Body)
|
||||||
|
return nil, fmt.Errorf("unexpected status code: %d, resp: %s", resp.StatusCode, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
var result struct {
|
||||||
|
Data []struct {
|
||||||
|
Embedding []float32 `json:"embedding"`
|
||||||
|
} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||||
|
return nil, fmt.Errorf("error decoding response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(result.Data) == 0 || len(result.Data[0].Embedding) == 0 {
|
||||||
|
return nil, fmt.Errorf("no embeddings returned")
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.Data[0].Embedding, nil
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"go-mod.ewintr.nl/henk/llm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
// startDir := "."
|
||||||
|
// err := filepath.Walk(startDir, walkFunc)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Fatalf("Error walking the path: %v\n", err)
|
||||||
|
// }
|
||||||
|
ollamaClient := llm.NewOllama("http://192.168.1.12:11434", "nomic-embed-text:latest", "qwen2.5-coder:3b-instruct-q8_0")
|
||||||
|
|
||||||
|
response, err := ollamaClient.Complete("You are a nice person.", "Say Hi!")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
func walkFunc(path string, info os.FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !info.IsDir() {
|
||||||
|
data, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error reading file %s: %v\n", path, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
fmt.Printf("Contents of file %s:\n%s\n", path, string(data))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in New Issue