Commit 96fa2c8b authored by Wade's avatar Wade

rag ok

parent a99c5c92
...@@ -132,35 +132,44 @@ curl -X POST http://localhost:8000/indexGraph \ ...@@ -132,35 +132,44 @@ curl -X POST http://localhost:8000/indexGraph \
curl -X POST http://localhost:8000/indexDocuments \
-H "Content-Type: application/json" \
-d '{"content": "What is the capital of UK?", "metadata": {"user_id": "user456", "username": "Bob"}}'
{"result": "Document indexed successfully"}
d1 := ai.DocumentFromText("Paris is the capital of France", nil)
d2 := ai.DocumentFromText("USA is the largest importer of coffee", nil)
d3 := ai.DocumentFromText("Water exists in 3 states - solid, liquid and gas", nil)
curl -X POST http://localhost:8000/indexDocuments \
-H "Content-Type: application/json" \
-d '{"content": "What is the capital of UK?", "metadata": {"user_id": "user456", "username": "Bob"}}'
{"result": "Paris is the capital of France"}
curl -X POST http://localhost:8000/indexDocuments \
-H "Content-Type: application/json" \
-d '{"content": "What is the capital of UK?", "metadata": {"user_id": "user456", "username": "Bob"}}'
{"result": "USA is the largest importer of coffee"}
curl -X POST http://localhost:8000/indexDocuments \
-H "Content-Type: application/json" \
-d '{"content": "Water exists in 3 states - solid, liquid and gas", "metadata": {"user_id": "user456", "username": "Bob"}}'
{"result": "USA is the largest importer of coffee"}
curl -X POST http://localhost:8000/indexDocuments \
-H "Content-Type: application/json" \
-d '{"content": "What is the capital of UK?", "metadata": {"user_id": "user456", "username": "Bob"}}'
{"result": "Document indexed successfully"}
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"strings"
"github.com/firebase/genkit/go/ai" "github.com/firebase/genkit/go/ai"
"github.com/firebase/genkit/go/genkit" "github.com/firebase/genkit/go/genkit"
...@@ -47,6 +48,16 @@ type GraphInput struct { ...@@ -47,6 +48,16 @@ type GraphInput struct {
Metadata map[string]interface{} `json:"metadata,omitempty"` Metadata map[string]interface{} `json:"metadata,omitempty"`
} }
const simpleQaPromptTemplate = `
You're a helpful agent that answers the user's common questions based on the context provided.
Here is the user's query: {{query}}
Here is the context you should use: {{context}}
Please provide the best answer you can.
`
func main() { func main() {
ctx := context.Background() ctx := context.Background()
...@@ -79,12 +90,12 @@ func main() { ...@@ -79,12 +90,12 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
m := ds.DefineModel(g, // m := ds.DefineModel(g,
deepseek.ModelDefinition{ // deepseek.ModelDefinition{
Name: "deepseek-chat", // Choose an appropriate model // Name: "deepseek-chat", // Choose an appropriate model
Type: "chat", // Must be chat for tool support // Type: "chat", // Must be chat for tool support
}, // },
nil) // nil)
embedder := googlegenai.GoogleAIEmbedder(g, "embedding-001") embedder := googlegenai.GoogleAIEmbedder(g, "embedding-001")
if embedder == nil { if embedder == nil {
...@@ -157,6 +168,16 @@ func main() { ...@@ -157,6 +168,16 @@ func main() {
return fmt.Sprintf("Document indexed successfully, docname %s", resDocName), nil return fmt.Sprintf("Document indexed successfully, docname %s", resDocName), nil
}) })
simpleQaPrompt, err := genkit.DefinePrompt(g, "simpleQaPrompt",
ai.WithModelName("googleai/gemini-2.0-flash"),
ai.WithPrompt(simpleQaPromptTemplate),
ai.WithInputType(simpleQaPromptInput{}),
ai.WithOutputFormat(ai.OutputFormatText),
)
if err != nil {
log.Fatal(err)
}
// Define a simple flow that generates jokes about a given topic // Define a simple flow that generates jokes about a given topic
genkit.DefineFlow(g, "chat", func(ctx context.Context, input *Input) (string, error) { genkit.DefineFlow(g, "chat", func(ctx context.Context, input *Input) (string, error) {
...@@ -175,29 +196,28 @@ func main() { ...@@ -175,29 +196,28 @@ func main() {
} }
for _, d := range response.Documents { for _, d := range response.Documents {
fmt.Println("d.Content[0].Text",d.Content[0].Text) fmt.Println("d.Content[0].Text", d.Content[0].Text)
} }
return "",nil var sb strings.Builder
for _, d := range response.Documents {
sb.WriteString(d.Content[0].Text)
resp, err := genkit.Generate(ctx, g, sb.WriteByte('\n')
ai.WithModel(m),
ai.WithPrompt(`Tell silly short jokes about apple`))
if err != nil {
fmt.Println(err.Error())
return "", err
} }
fmt.Println("resp.Text()", resp.Text()) promptInput := &simpleQaPromptInput{
Query: input.Content,
Context: sb.String(),
}
resp, err := simpleQaPrompt.Execute(ctx, ai.WithInput(promptInput))
if err != nil { if err != nil {
return "", err return "", err
} }
return resp.Text(), nil
text := resp.Text() //ai.WithPrompt(promptInput))
return text, nil //ai.WithPrompt(`Tell silly short jokes about apple`)
}) })
// 配置限速器:每秒 10 次请求,突发容量 20,最大并发 5 // 配置限速器:每秒 10 次请求,突发容量 20,最大并发 5
...@@ -225,3 +245,8 @@ func main() { ...@@ -225,3 +245,8 @@ func main() {
log.Fatalf("Server failed: %v", err) log.Fatalf("Server failed: %v", err)
} }
} }
type simpleQaPromptInput struct {
Query string `json:"query"`
Context string `json:"context"`
}
...@@ -13,7 +13,7 @@ import ( ...@@ -13,7 +13,7 @@ import (
// func TestGenerateEmbedding(t *testing.T) // func TestGenerateEmbedding(t *testing.T)
func TestGenerateEmbedding(t *testing.T){ func TestGenerateEmbedding(t *testing.T) {
ctx := context.Background() ctx := context.Background()
// Initialize Genkit with Google AI plugin // Initialize Genkit with Google AI plugin
......
...@@ -265,10 +265,6 @@ type docStore struct { ...@@ -265,10 +265,6 @@ type docStore struct {
// }, nil // }, nil
// } // }
// newDocStore creates a docStore. // newDocStore creates a docStore.
func (m *Milvus) newDocStore(ctx context.Context, cfg *CollectionConfig) (*docStore, error) { func (m *Milvus) newDocStore(ctx context.Context, cfg *CollectionConfig) (*docStore, error) {
if m.client == nil { if m.client == nil {
...@@ -389,15 +385,6 @@ func (m *Milvus) newDocStore(ctx context.Context, cfg *CollectionConfig) (*docSt ...@@ -389,15 +385,6 @@ func (m *Milvus) newDocStore(ctx context.Context, cfg *CollectionConfig) (*docSt
}, nil }, nil
} }
// Indexer returns the indexer for a collection. // Indexer returns the indexer for a collection.
func Indexer(g *genkit.Genkit, collection string) ai.Indexer { func Indexer(g *genkit.Genkit, collection string) ai.Indexer {
return genkit.LookupIndexer(g, provider, collection) return genkit.LookupIndexer(g, provider, collection)
...@@ -472,10 +459,6 @@ func (ds *docStore) Index(ctx context.Context, req *ai.IndexerRequest) error { ...@@ -472,10 +459,6 @@ func (ds *docStore) Index(ctx context.Context, req *ai.IndexerRequest) error {
return nil return nil
} }
// // RetrieverOptions for Milvus retrieval. // // RetrieverOptions for Milvus retrieval.
// type RetrieverOptions struct { // type RetrieverOptions struct {
// Count int `json:"count,omitempty"` // Max documents to retrieve. // Count int `json:"count,omitempty"` // Max documents to retrieve.
...@@ -653,14 +636,6 @@ func (ds *docStore) Index(ctx context.Context, req *ai.IndexerRequest) error { ...@@ -653,14 +636,6 @@ func (ds *docStore) Index(ctx context.Context, req *ai.IndexerRequest) error {
// return strings.Join(strs, sep) // return strings.Join(strs, sep)
// } // }
// RetrieverOptions for Milvus retrieval. // RetrieverOptions for Milvus retrieval.
type RetrieverOptions struct { type RetrieverOptions struct {
Count int `json:"count,omitempty"` // Max documents to retrieve. Count int `json:"count,omitempty"` // Max documents to retrieve.
...@@ -787,4 +762,3 @@ func (ds *docStore) Retrieve(ctx context.Context, req *ai.RetrieverRequest) (*ai ...@@ -787,4 +762,3 @@ func (ds *docStore) Retrieve(ctx context.Context, req *ai.RetrieverRequest) (*ai
Documents: docs, Documents: docs,
}, nil }, nil
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment