Commit 39af445c authored by Wade's avatar Wade

chat grap api ok

parent e410aba4
......@@ -2,6 +2,19 @@
curl -X POST \
-H "Content-Type: application/json" \
-d '{"content": "What is the capital of UK?", "username": "Alice", "user_id": "user123"}' \
http://localhost:8000/chat
# milvus
......@@ -9,6 +22,28 @@ http://54.92.111.204:9091/webui/collections
curl -X POST http://localhost:8000/indexDocuments \
-H "Content-Type: application/json" \
-d '{"user_id": "user123", "username": "Alice", "content": "Paris is the capital of France?", "metadata": {}}'
curl -X POST http://localhost:8000/indexDocuments \
-H "Content-Type: application/json" \
-d '{"user_id": "user456", "username": "Bob", "content": "Paris is the capital of France?", "metadata": {}}'
curl -X POST http://localhost:8000/indexDocuments \
-H "Content-Type: application/json" \
-d '{"user_id": "user456", "username": "Bob", "content": "Paris is the capital of France?", "metadata": {}}'
curl -X POST http://localhost:8000/indexDocuments \
-H "Content-Type: application/json" \
-d '{"user_id": "user456", "username": "Bob", "content": "USA is the largest importer of coffee?"}'
curl -X POST http://localhost:8000/indexDocuments \
-H "Content-Type: application/json" \
-d '{"content": "Paris is the capital of France?", "metadata": {"user_id": "user456", "username": "Bob"}}'
......
......@@ -29,14 +29,22 @@ paths:
type: string
description: The API key for authentication
example: "sk-1234567890abcdef"
username:
from:
type: string
description: The username of the requester
example: "john_doe"
user_id:
description: The sender of the chat message
example: "Alice"
from_id:
type: string
description: The unique identifier for the user
example: "user_12345"
description: The unique identifier for the sender
example: "user123"
to:
type: string
description: The recipient of the chat message
example: "Bob"
to_id:
type: string
description: The unique identifier for the recipient
example: "user456"
required:
- content
responses:
......@@ -132,10 +140,10 @@ paths:
type: string
description: Error message
example: "Failed to store data"
/idx/graphrag:
/index/graph:
post:
summary: Store GraphRAG index data
description: Stores question, answer, and summary data for GraphRAG indexing.
description: Stores content data for GraphRAG indexing, including user information and optional metadata.
tags:
- Indexing
requestBody:
......@@ -145,29 +153,27 @@ paths:
schema:
type: object
properties:
question:
type: string
description: The question to store
example: "What is NLP?"
answer:
type: string
description: The answer to store
example: "NLP is natural language processing..."
summary:
content:
type: string
description: The summary of the Q&A
example: "NLP overview"
description: The content to store for indexing
example: "Paris is the capital of France"
username:
type: string
description: The username of the requester
example: "john_doe"
example: "bob"
user_id:
type: string
description: The unique identifier for the user
example: "user_12345"
example: "user456"
metadata:
type: object
description: Additional metadata for the content
additionalProperties: true
example:
source: "user_input"
timestamp: "2025-06-04T16:54:00+08:00"
required:
- question
- answer
- content
responses:
'200':
description: Successful storage of GraphRAG index data
......@@ -202,56 +208,5 @@ paths:
type: string
description: Error message
example: "Failed to store data"
/index:
post:
summary: Trigger indexing of existing QA data
description: Triggers the indexing process for existing QA data in the database using the pgvector extension.
tags:
- Indexing
requestBody:
required: false
content:
application/json:
schema:
type: object
properties:
apiKey:
type: string
description: The API key for authentication
example: "sk-1234567890abcdef"
responses:
'200':
description: Indexing process completed successfully
content:
application/json:
schema:
type: object
properties:
message:
type: string
description: Success message
example: "Indexing completed successfully"
'400':
description: Invalid input
content:
application/json:
schema:
type: object
properties:
error:
type: string
description: Error message
example: "Invalid API key"
'500':
description: Server error
content:
application/json:
schema:
type: object
properties:
error:
type: string
description: Error message
example: "Failed to index data"
components:
schemas: {}
......@@ -24,20 +24,21 @@ import (
"github.com/wade-liwei/agentchat/util"
)
// GraphKnowledge
type Input struct {
Content string `json:"content,omitempty"`
Model string `json:"model,omitempty"`
APIKey string `json:"apiKey,omitempty"`
Username string `json:"username,omitempty"`
UserID string `json:"user_id,omitempty"`
type ChatInput struct {
Content string `json:"content,omitempty"`
Model string `json:"model,omitempty"`
APIKey string `json:"apiKey,omitempty"`
From string `json:"from,omitempty"` // 替换 Username
FromID string `json:"from_id,omitempty"` // 替换 UserID
To string `json:"to,"`
ToID string `json:"to_id,omitempty"`
}
// DocumentInput 结构体用于文档索引接口
type DocumentInput struct {
// UserID string `json:"user_id"`
// Username string `json:"username"`
UserID string `json:"user_id"`
Username string `json:"username"`
Content string `json:"content"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
......@@ -106,7 +107,7 @@ func main() {
// Configure collection
cfg := milvus.CollectionConfig{
Collection: "chatRag",
Collection: "chatRag1",
Dimension: 768, // Match mock embedder dimension
Embedder: embedder,
EmbedderOptions: map[string]interface{}{}, // Explicitly set as map
......@@ -122,6 +123,15 @@ func main() {
// 定义文档索引流
genkit.DefineFlow(g, "indexDocuments", func(ctx context.Context, input *DocumentInput) (string, error) {
if input.Metadata == nil{
input.Metadata =make(map[string]any)
}
input.Metadata[util.UserIdKey]=input.UserID
input.Metadata[util.UserNameKey]= input.Username
doc := ai.DocumentFromText(input.Content, input.Metadata)
err := indexer.Index(ctx, &ai.IndexerRequest{
Documents: []*ai.Document{doc},
......@@ -136,7 +146,7 @@ func main() {
_ = graphRetriever
genkit.DefineFlow(g, "indexGraph", func(ctx context.Context, input *GraphInput) (string, error) {
genkit.DefineFlow(g, "index/graph", func(ctx context.Context, input *GraphInput) (string, error) {
opt := graphrag.IndexReqOption{
UserId: input.UserID,
......@@ -180,8 +190,14 @@ func main() {
log.Fatal(err)
}
qa,err :=InitQAStore()
if err != nil{
log.Fatalf("InitQAStore failed: %v", err)
}
// 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 *ChatInput) (string, error) {
inputAsJson, err := json.Marshal(input)
......@@ -191,10 +207,24 @@ func main() {
fmt.Println("input-------------------------------", string(inputAsJson))
metaData := make(map[string]any)
metaData[util.UserIdKey]= input.UserID
metaData[util.UserNameKey]= input.Username
qa.WriteQA(context.Background(),QA{
FromID: &input.FromID, //*string // 可空的 from_id
From: &input.From, //*string // 可空的 from
Question: &input.Content, //*string // 可空的问题
//Answer: //*string // 可空的答案
//Summary //*string // 可空的摘要
To: &input.To, //*string // 可空的 to
ToID: &input.ToID, //*string // 可空的 to_id
})
//qa.GetLatestQA(context.Background(),&input.FromID)
metaData := make(map[string]any)
metaData[util.UserIdKey]= input.ToID
metaData[util.UserNameKey]= input.To
dRequest := ai.DocumentFromText(input.Content, metaData)
response, err := ai.Retrieve(ctx, retriever, ai.WithDocs(dRequest))
......
......@@ -388,16 +388,6 @@ type docStore struct {
package graphrag
import (
"context"
"fmt"
"github.com/milvus-io/milvus-sdk-go/v2/entity"
"github.com/pkg/errors"
)
// newDocStore creates a docStore.
func (m *Milvus) newDocStore(ctx context.Context, cfg *CollectionConfig) (*docStore, error) {
if m.client == nil {
......@@ -543,21 +533,20 @@ func (ds *docStore) Index(ctx context.Context, req *ai.IndexerRequest) error {
if doc.Metadata == nil {
// If ok, we don't use the User struct since the requirement is to error on non-nil
return nil, fmt.Errorf("req.Query.Metadata must be not nil, got type %T", req.Options)
return fmt.Errorf("req.Query.Metadata must be not nil, got type %T", req.Options)
}
// Extract username and user_id from req.Query.Metadata
userName, ok := doc.Metadata[util.UserNameKey].(string)
if !ok {
return nil, fmt.Errorf("req.Query.Metadata must provide username key")
return fmt.Errorf("req.Query.Metadata must provide username key")
}
userId, ok := doc.Metadata[util.UserIdKey].(string)
if !ok {
return nil, fmt.Errorf("req.Query.Metadata must provide user_id key")
return fmt.Errorf("req.Query.Metadata must provide user_id key")
}
var sb strings.Builder
for _, p := range doc.Content {
if p.IsText() {
......@@ -580,7 +569,7 @@ func (ds *docStore) Index(ctx context.Context, req *ai.IndexerRequest) error {
rows = append(rows, row)
// Debug: Log row contents.
fmt.Printf("Row %d: vector_len=%d, text=%q,userId=%s,username=%s,metadata=%v\n", i, len(emb.Embedding), text,userId,userName metadata)
fmt.Printf("Row %d: vector_len=%d, text=%q,userId=%s,username=%s,metadata=%v\n", i, len(emb.Embedding), text,userId,userName,metadata)
}
// Debug: Log total rows.
......
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