Skip to content

Commit df1b696

Browse files
authored
supporting content for ollama-and-go-for-rag (#344)
1 parent 4793d83 commit df1b696

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package elasticsearch
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"strings"
8+
9+
"github.com/elastic/go-elasticsearch/v8"
10+
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
11+
)
12+
13+
// Initializing elasticsearch client
14+
15+
func EsClient() (*elasticsearch.TypedClient, error) {
16+
var cloudID = "" // your Elastic Cloud ID Here
17+
var apiKey = "" // your Elastic ApiKey Here
18+
19+
es, err := elasticsearch.NewTypedClient(elasticsearch.Config{
20+
CloudID: cloudID,
21+
APIKey: apiKey,
22+
})
23+
24+
if err != nil {
25+
return nil, fmt.Errorf("unable to connect: %w", err)
26+
}
27+
return es, nil
28+
}
29+
30+
// Searching for documents and building the context
31+
func SemanticRetriever(client *elasticsearch.TypedClient, query string, size int) (string, error) {
32+
// Perform the semantic search
33+
res, err := client.Search().
34+
Index("rag-ollama").
35+
Query(&types.Query{
36+
Semantic: &types.SemanticQuery{
37+
Field: "semantic_field",
38+
Query: query,
39+
},
40+
}).
41+
Size(size).
42+
Do(context.Background())
43+
44+
if err != nil {
45+
return "", fmt.Errorf("semantic search failed: %w", err)
46+
}
47+
48+
// Prepare to format the results
49+
var output strings.Builder
50+
output.WriteString("Documents found\n\n")
51+
52+
// Iterate through the search hits
53+
for i, hit := range res.Hits.Hits {
54+
// Define a struct to unmarshal each document
55+
var doc struct {
56+
Title string `json:"title"`
57+
Content string `json:"content"`
58+
}
59+
60+
// Unmarshal the document source into our struct
61+
if err := json.Unmarshal(hit.Source_, &doc); err != nil {
62+
return "", fmt.Errorf("failed to unmarshal document %d: %w", i, err)
63+
}
64+
65+
// Append the formatted document to our output
66+
output.WriteString(fmt.Sprintf("Title\n%s\n\nContent\n%s\n", doc.Title, doc.Content))
67+
68+
// Add a separator between documents, except for the last one
69+
if i < len(res.Hits.Hits)-1 {
70+
output.WriteString("\n-----\n\n")
71+
}
72+
}
73+
74+
// Return the formatted output as a string
75+
return output.String(), nil
76+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module ollama-rag
2+
3+
go 1.23.2
4+
5+
require (
6+
github.com/elastic/elastic-transport-go/v8 v8.6.0 // indirect
7+
github.com/elastic/go-elasticsearch/v8 v8.15.0 // indirect
8+
github.com/go-logr/logr v1.4.1 // indirect
9+
github.com/go-logr/stdr v1.2.2 // indirect
10+
github.com/parakeet-nest/parakeet v0.2.1 // indirect
11+
go.opentelemetry.io/otel v1.24.0 // indirect
12+
go.opentelemetry.io/otel/metric v1.24.0 // indirect
13+
go.opentelemetry.io/otel/trace v1.24.0 // indirect
14+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
github.com/elastic/elastic-transport-go/v8 v8.6.0 h1:Y2S/FBjx1LlCv5m6pWAF2kDJAHoSjSRSJCApolgfthA=
2+
github.com/elastic/elastic-transport-go/v8 v8.6.0/go.mod h1:YLHer5cj0csTzNFXoNQ8qhtGY1GTvSqPnKWKaqQE3Hk=
3+
github.com/elastic/go-elasticsearch/v8 v8.15.0 h1:IZyJhe7t7WI3NEFdcHnf6IJXqpRf+8S8QWLtZYYyBYk=
4+
github.com/elastic/go-elasticsearch/v8 v8.15.0/go.mod h1:HCON3zj4btpqs2N1jjsAy4a/fiAul+YBP00mBH4xik8=
5+
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
6+
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
7+
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
8+
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
9+
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
10+
github.com/parakeet-nest/parakeet v0.2.1 h1:JVw73nDhQUtl+FN7X4s23oqqSv6Wg/+KEaQhhpxzRGg=
11+
github.com/parakeet-nest/parakeet v0.2.1/go.mod h1:8M3ApjoLs/+tFuAqCxE2g9iSAODDT3OoW/Z6c5z++gw=
12+
go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo=
13+
go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo=
14+
go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI=
15+
go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco=
16+
go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI=
17+
go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU=
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"ollama-rag/elasticsearch"
7+
8+
"github.com/parakeet-nest/parakeet/completion"
9+
"github.com/parakeet-nest/parakeet/enums/option"
10+
"github.com/parakeet-nest/parakeet/llm"
11+
)
12+
13+
func main() {
14+
15+
ollamaUrl := "http://localhost:11434"
16+
chatModel := "llama3.2:latest"
17+
question := `Summarize document: JAK Inhibitors vs. Monoclonal Antibodies in Rheumatoid Arthritis Treatment`
18+
size := 3
19+
20+
esClient, err := elasticsearch.EsClient()
21+
22+
if err != nil {
23+
log.Fatalln("😡:", err)
24+
}
25+
26+
// Retrieve documents from semantic query to build context
27+
documentsContent, nil := elasticsearch.SemanticRetriever(esClient, question, size)
28+
29+
systemContent := `You are a helpful medical assistant. Only answer the questions based on found documents.
30+
Add references to the base document titles and be succint in your answers.`
31+
32+
options := llm.SetOptions(map[string]interface{}{
33+
option.Temperature: 0.0,
34+
})
35+
36+
queryChat := llm.Query{
37+
Model: chatModel,
38+
Messages: []llm.Message{
39+
{Role: "system", Content: systemContent},
40+
{Role: "system", Content: documentsContent},
41+
{Role: "user", Content: question},
42+
},
43+
Options: options,
44+
}
45+
46+
fmt.Println()
47+
fmt.Println("🤖 answer:")
48+
49+
// Answer the question
50+
_, err = completion.ChatStream(ollamaUrl, queryChat,
51+
func(answer llm.Answer) error {
52+
fmt.Print(answer.Message.Content)
53+
return nil
54+
})
55+
if err != nil {
56+
log.Fatal("😡:", err)
57+
}
58+
59+
fmt.Println()
60+
}

0 commit comments

Comments
 (0)