Commit bd287386 authored by Wade's avatar Wade

summary ok

parent afaa731b
This diff is collapsed.
...@@ -23,7 +23,6 @@ func getPackageName() string { ...@@ -23,7 +23,6 @@ func getPackageName() string {
return pkg return pkg
} }
func loggingInit() { func loggingInit() {
// debug := flag.Bool("debug", false, "sets log level to debug") // debug := flag.Bool("debug", false, "sets log level to debug")
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"net/http" "net/http"
"os"
"strings" "strings"
"time" "time"
...@@ -14,6 +15,7 @@ import ( ...@@ -14,6 +15,7 @@ import (
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/wade-liwei/agentchat/plugins/deepseek" "github.com/wade-liwei/agentchat/plugins/deepseek"
"github.com/wade-liwei/agentchat/plugins/graphrag" "github.com/wade-liwei/agentchat/plugins/graphrag"
"github.com/wade-liwei/agentchat/plugins/knowledge" // Import knowledge package
"github.com/wade-liwei/agentchat/plugins/milvus" "github.com/wade-liwei/agentchat/plugins/milvus"
"github.com/firebase/genkit/go/plugins/evaluators" "github.com/firebase/genkit/go/plugins/evaluators"
...@@ -76,7 +78,6 @@ type simpleQaPromptInput struct { ...@@ -76,7 +78,6 @@ type simpleQaPromptInput struct {
// Please provide a response that aligns with the given personality while leveraging the provided context, graph, and conversation summary. // Please provide a response that aligns with the given personality while leveraging the provided context, graph, and conversation summary.
// ` // `
const simpleQaPromptTemplate = ` const simpleQaPromptTemplate = `
You're a helpful agent that answers the user's questions based on the provided context. You're a helpful agent that answers the user's questions based on the provided context.
...@@ -94,18 +95,16 @@ Instructions: ...@@ -94,18 +95,16 @@ Instructions:
- Ensure responses leverage the Previous conversation summary when relevant. - Ensure responses leverage the Previous conversation summary when relevant.
` `
func main() { func main() {
// Define command-line flags with hardcoded values as defaults
// Define command-line flags with hardcoded values as defaults deepseekAPIKey := flag.String("deepseek-api-key", "sk-9f70df871a7c4b8aa566a3c7a0603706", "DeepSeek API key")
deepseekAPIKey := flag.String("deepseek-api-key", "sk-9f70df871a7c4b8aa566a3c7a0603706", "DeepSeek API key") milvusAddr := flag.String("milvus-addr", "54.92.111.204:19530", "Milvus server address")
milvusAddr := flag.String("milvus-addr", "54.92.111.204:19530", "Milvus server address") graphragAddr := flag.String("graphrag-addr", "54.92.111.204:5670", "GraphRAG server address")
graphragAddr := flag.String("graphrag-addr", "54.92.111.204:5670", "GraphRAG server address") googleAIApiKey := flag.String("googleai-api-key", "AIzaSyCoYBOmnwRWlH_-nT25lpn8pMg3T18Q0uI", "Google AI API key")
googleAIApiKey := flag.String("googleai-api-key", "AIzaSyCoYBOmnwRWlH_-nT25lpn8pMg3T18Q0uI", "Google AI API key")
pgConnString := flag.String("pg-conn-string", "postgresql://postgres.awcfgdodiuqnlsobcivq:P99IU9NEoDRPsBfb@aws-0-ap-southeast-1.pooler.supabase.com:5432/postgres", "PostgreSQL connection string") pgConnString := flag.String("pg-conn-string", "postgresql://postgres.awcfgdodiuqnlsobcivq:P99IU9NEoDRPsBfb@aws-0-ap-southeast-1.pooler.supabase.com:5432/postgres", "PostgreSQL connection string")
debug := flag.Bool("debug", false, "sets log level to debug") debug := flag.Bool("debug", false, "sets log level to debug")
flag.Parse() flag.Parse()
loggingInit() loggingInit()
...@@ -113,6 +112,9 @@ func main() { ...@@ -113,6 +112,9 @@ func main() {
zerolog.SetGlobalLevel(zerolog.DebugLevel) zerolog.SetGlobalLevel(zerolog.DebugLevel)
} }
os.Setenv("TENCENTCLOUD_SECRET_ID", "AKID64oLfmfLtESUJ6i8LPSM4gCVbiniQuBF")
os.Setenv("TENCENTCLOUD_SECRET_KEY", "rX2JMBnBMJ2YqulOo37xa5OUMSN4Xnpd")
ctx := context.Background() ctx := context.Background()
metrics := []evaluators.MetricConfig{ metrics := []evaluators.MetricConfig{
{ {
...@@ -126,14 +128,14 @@ func main() { ...@@ -126,14 +128,14 @@ func main() {
}, },
} }
// Initialize genkit with plugins using flag/env values // Initialize genkit with plugins using flag/env values
g, err := genkit.Init(ctx, genkit.WithPlugins( g, err := genkit.Init(ctx, genkit.WithPlugins(
&deepseek.DeepSeek{APIKey: *deepseekAPIKey}, &deepseek.DeepSeek{APIKey: *deepseekAPIKey},
&milvus.Milvus{Addr: *milvusAddr}, &milvus.Milvus{Addr: *milvusAddr},
&graphrag.GraphKnowledge{Addr: *graphragAddr}, &graphrag.GraphKnowledge{Addr: *graphragAddr},
&googlegenai.GoogleAI{APIKey: *googleAIApiKey}, &googlegenai.GoogleAI{APIKey: *googleAIApiKey},
&evaluators.GenkitEval{Metrics: metrics}, &evaluators.GenkitEval{Metrics: metrics},
)) ))
if err != nil { if err != nil {
log.Fatal().Msg(err.Error()) log.Fatal().Msg(err.Error())
...@@ -250,6 +252,16 @@ func main() { ...@@ -250,6 +252,16 @@ func main() {
log.Fatal().Msgf("InitQAStore failed: %v", err) log.Fatal().Msgf("InitQAStore failed: %v", err)
} }
// Initialize KnowledgeClient with test parameters
kc := knowledge.NewKnowledgeClient(knowledge.ClientConfig{
Endpoint: "lkeap.tencentcloudapi.com",
Region: "ap-guangzhou",
})
if err := kc.Init(ctx); err != nil {
log.Fatal().Msgf("Failed to initialize KnowledgeClient: %v", err)
}
log.Info().Msg("KnowledgeClient initialized successfully")
// 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 *ChatInput) (Response, error) { genkit.DefineFlow(g, "chat", func(ctx context.Context, input *ChatInput) (Response, error) {
...@@ -319,7 +331,6 @@ func main() { ...@@ -319,7 +331,6 @@ func main() {
begin := time.Now() begin := time.Now()
graphResponse, err := ai.Retrieve(ctx, graphRetriever, ai.WithDocs(dRequest)) graphResponse, err := ai.Retrieve(ctx, graphRetriever, ai.WithDocs(dRequest))
if err != nil { if err != nil {
log.Error().Msgf("graph Retrieve err.Error() %s", err.Error()) log.Error().Msgf("graph Retrieve err.Error() %s", err.Error())
...@@ -333,10 +344,7 @@ func main() { ...@@ -333,10 +344,7 @@ func main() {
log.Info().Msgf("promptInput.Graph : %s", promptInput.Graph) log.Info().Msgf("promptInput.Graph : %s", promptInput.Graph)
} }
fmt.Println("graph time", time.Since(begin).Seconds())
fmt.Println("graph time",time.Since(begin).Seconds())
resp, err := simpleQaPrompt.Execute(ctx, ai.WithInput(promptInput)) resp, err := simpleQaPrompt.Execute(ctx, ai.WithInput(promptInput))
...@@ -347,18 +355,46 @@ func main() { ...@@ -347,18 +355,46 @@ func main() {
}, nil }, nil
} }
if lastok {
if promptInput.Summary == ""{
promptInput.Summary = resp.Text()
}
qa.UpdateQAFields(context.Background(), idx, "", resp.Text()) log.Info().
Str("from",input.From).
Str("from_id",input.FromID).
Str("to",input.To).
Str("to_id",input.ToID).
Str("promptInput.Query",promptInput.Query).
Str("resp.Text()",resp.Text()).
Str("promptInput.Summary",promptInput.Summary).Msg("QueryRewriteWithSummary")
res, err := kc.QueryRewriteWithSummary(context.Background(), promptInput.Query, resp.Text(), promptInput.Summary)
if err != nil {
log.Error().Msg(err.Error())
} else {
qa.UpdateQAFields(context.Background(), idx, res.RewrittenQuery, resp.Text())
/*
{"RewrittenQuery":"Conversation summary: The available knowledge base does not contain information about the capital of the UK.","RawResponse":{"Response":{"Content":"Conversation summary: The available knowledge base does not contain information about the capital of the UK.","Usage":{"InputTokens":74,"OutputTokens":19,"TotalTokens":93},"RequestId":"15f1ce0c-a83f-4d95-af22-33a3bd829e8d"}}}
*/
}
} else {
qa.UpdateQAFields(context.Background(), idx, "", resp.Text())
}
log.Info(). log.Info().
Str("question",promptInput.Query). Str("from",input.From).
Str("context",promptInput.Context). Str("from_id",input.FromID).
Str("graph",promptInput.Graph). Str("to",input.To).
Str("last summary",promptInput.Summary). Str("to_id",input.ToID).
Str("answer", resp.Text()). Str("question", promptInput.Query).
Msg("Question and answer pair recorded") Str("context", promptInput.Context).
Str("graph", promptInput.Graph).
Str("last summary", promptInput.Summary).
Str("answer", resp.Text()).
Msg("Question and answer pair recorded")
return Response{ return Response{
Data: resp.Text(), Data: resp.Text(),
...@@ -392,7 +428,6 @@ func main() { ...@@ -392,7 +428,6 @@ func main() {
} }
} }
type Response struct { type Response struct {
Data string `json:"data"` Data string `json:"data"`
Code int `json:"code"` Code int `json:"code"`
......
This diff is collapsed.
This diff is collapsed.
...@@ -10,41 +10,40 @@ import ( ...@@ -10,41 +10,40 @@ import (
) )
func main() { func main() {
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密 // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性 // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性
// 以下代码示例仅供参考,建议采用更安全的方式来使用密钥 // 以下代码示例仅供参考,建议采用更安全的方式来使用密钥
// 请参见:https://cloud.tencent.com/document/product/1278/85305 // 请参见:https://cloud.tencent.com/document/product/1278/85305
// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取 // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
//os.Setenv("TENCENTCLOUD_SECRET_ID", "AKID64oLfmfLtESUJ6i8LPSM4gCVbiniQuBF")
//os.Setenv("TENCENTCLOUD_SECRET_KEY", "rX2JMBnBMJ2YqulOo37xa5OUMSN4Xnpd")
//os.Setenv("TENCENTCLOUD_SECRET_ID", "AKID64oLfmfLtESUJ6i8LPSM4gCVbiniQuBF") credential := common.NewCredential(
//os.Setenv("TENCENTCLOUD_SECRET_KEY", "rX2JMBnBMJ2YqulOo37xa5OUMSN4Xnpd") "AKID64oLfmfLtESUJ6i8LPSM4gCVbiniQuBF",
"rX2JMBnBMJ2YqulOo37xa5OUMSN4Xnpd",
)
// 使用临时密钥示例
// credential := common.NewTokenCredential("SecretId", "SecretKey", "Token")
// 实例化一个client选项,可选的,没有特殊需求可以跳过
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "lkeap.tencentcloudapi.com"
// 实例化要请求产品的client对象,clientProfile是可选的
client, _ := lkeap.NewClient(credential, "ap-guangzhou", cpf)
credential := common.NewCredential( // 实例化一个请求对象,每个接口都会对应一个request对象
"AKID64oLfmfLtESUJ6i8LPSM4gCVbiniQuBF", request := lkeap.NewQueryRewriteRequest()
"rX2JMBnBMJ2YqulOo37xa5OUMSN4Xnpd",
)
// 使用临时密钥示例
// credential := common.NewTokenCredential("SecretId", "SecretKey", "Token")
// 实例化一个client选项,可选的,没有特殊需求可以跳过
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "lkeap.tencentcloudapi.com"
// 实例化要请求产品的client对象,clientProfile是可选的
client, _ := lkeap.NewClient(credential, "ap-guangzhou", cpf)
// 实例化一个请求对象,每个接口都会对应一个request对象 request.Model = common.StringPtr("*")
request := lkeap.NewQueryRewriteRequest() // 返回的resp是一个QueryRewriteResponse的实例,与请求对象对应
response, err := client.QueryRewrite(request)
request.Model = common.StringPtr("*") if _, ok := err.(*errors.TencentCloudSDKError); ok {
// 返回的resp是一个QueryRewriteResponse的实例,与请求对象对应 fmt.Printf("An API error has returned: %s", err)
response, err := client.QueryRewrite(request) return
if _, ok := err.(*errors.TencentCloudSDKError); ok { }
fmt.Printf("An API error has returned: %s", err) if err != nil {
return panic(err)
} }
if err != nil { // 输出json格式的字符串回包
panic(err) fmt.Printf("%s", response.ToJsonString())
} }
// 输出json格式的字符串回包
fmt.Printf("%s", response.ToJsonString())
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
_ "github.com/lib/pq" _ "github.com/lib/pq"
) )
type QA struct { type QA struct {
ID int64 // 主键 ID int64 // 主键
CreatedAt time.Time // 创建时间 CreatedAt time.Time // 创建时间
......
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