Commit 8c52cd01 authored by Wade's avatar Wade

kc

parent 2337a5b9
......@@ -229,7 +229,7 @@ func DefineDocumentFlow(g *genkit.Genkit, indexer ai.Indexer) {
})
}
func DefineChatFlow(g *genkit.Genkit, retriever ai.Retriever, graphRetriever ai.Retriever, pgConnString string,kc *knowledge.KnowledgeClient ) {
func DefineChatFlow(g *genkit.Genkit, retriever ai.Retriever, graphRetriever ai.Retriever, pgConnString string, kc *knowledge.KnowledgeClient) {
qa, err := question.InitQAStore(pgConnString)
if err != nil {
......@@ -240,9 +240,7 @@ func DefineChatFlow(g *genkit.Genkit, retriever ai.Retriever, graphRetriever ai.
genkit.DefineFlow(g, "chat", func(ctx context.Context, input *ChatInput) (Response, error) {
ctxAsJson, _ := json.Marshal(ctx)
log.Info().Msgf("input----ctxAsJson----%s", string(ctxAsJson))
inputAsJson, err := json.Marshal(input)
if err != nil {
......@@ -435,41 +433,52 @@ func defineSimpleQaPrompt(g *genkit.Genkit, promptName string) (*ai.Prompt, erro
return simpleQaPrompt, nil
}
func DefineSplitDocFlow(g *genkit.Genkit,kc *knowledge.KnowledgeClient ) {
func DefineSplitDocFlow(g *genkit.Genkit, kc *knowledge.KnowledgeClient) {
// Define a simple flow that generates jokes about a given topic
genkit.DefineFlow(g, "split/document", func(ctx context.Context, input string) (SplitDocResponse, error) {
res,err := kc.ReconstructDocumentSSE(ctx,input)
res, err := kc.ReconstructDocumentSSE(ctx, input)
if err != nil{
if err != nil {
return SplitDocResponse{
Code: 500,
Msg: err.Error(),
},nil
}, nil
}
for k,v := range res{
fmt.Println("process",k,*v.Progress)
for k, v := range res {
fmt.Println("process", k, *v.Progress)
}
return SplitDocResponse{
Code: 200,
Data: res,
},nil
}, nil
})
}
type Response struct {
Data string `json:"data"`
Code int `json:"code"`
Msg string `json:"msg"`
}
type SplitDocResponse struct {
Data []knowledge.ReconstructDocumentSSEResponse `json:"data"`
Code int `json:"code"`
Msg string `json:"msg"`
}
const simpleQaPromptTemplate = `
You're a helpful agent that answers the user's questions based on the provided context.
Here is the user's query: {{query}}
Here is the context you should use: {{context}} from Milvus
Graph context: {{graph}}
Previous conversation summary: {{summary}}
Instructions:
- If the query is related to a character's personality, adopt the tone and style specified in the Personality context, and generate a response using the Milvus and Graph contexts to inform the personality-driven content.
- For all other queries, provide a clear and accurate response using the Milvus and Graph contexts, without emphasizing the Personality context.
- Ensure responses leverage the Previous conversation summary when relevant.
`
......@@ -59,39 +59,6 @@ type simpleQaPromptInput struct {
Summary string `json:"summary"`
}
// const simpleQaPromptTemplate = `
// You're a helpful agent that answers the user's questions with a tone and style shaped by the specified personality.
// Here is the user's query: {{query}}
// Here is the context you should use: {{context}} from Milvus
// Graph context: {{graph}}
// Previous conversation summary: {{summary}}
// Personality to adopt: {{personality}}
// Please provide a response that aligns with the given personality while leveraging the provided context, graph, and conversation summary.
// `
const simpleQaPromptTemplate = `
You're a helpful agent that answers the user's questions based on the provided context.
Here is the user's query: {{query}}
Here is the context you should use: {{context}} from Milvus
Graph context: {{graph}}
Previous conversation summary: {{summary}}
Instructions:
- If the query is related to a character's personality, adopt the tone and style specified in the Personality context, and generate a response using the Milvus and Graph contexts to inform the personality-driven content.
- For all other queries, provide a clear and accurate response using the Milvus and Graph contexts, without emphasizing the Personality context.
- Ensure responses leverage the Previous conversation summary when relevant.
`
func main() {
// Define command-line flags with hardcoded values as defaults
......@@ -163,8 +130,6 @@ func main() {
log.Fatal().Msgf("graphrag.DefineIndexerAndRetriever failed: %v", err)
}
DefineDocumentFlow(g, indexer)
DefineGraphFlow(g, graphIndexer)
DefineModelsFlow(g)
......@@ -179,8 +144,8 @@ func main() {
}
log.Info().Msg("KnowledgeClient initialized successfully")
DefineChatFlow(g, retriever, graphRetriever, *pgConnString,kc)
DefineSplitDocFlow(g,kc)
DefineChatFlow(g, retriever, graphRetriever, *pgConnString, kc)
DefineSplitDocFlow(g, kc)
// 配置限速器:每秒 10 次请求,突发容量 20,最大并发 5
rl := NewRateLimiter(10, 20, 5)
......@@ -207,4 +172,3 @@ func main() {
log.Fatal().Msgf("Server failed: %v", err)
}
}
package main
import (
"fmt"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
lkeap "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lkeap/v20240522"
)
func main() {
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性
// 以下代码示例仅供参考,建议采用更安全的方式来使用密钥
// 请参见:https://cloud.tencent.com/document/product/1278/85305
// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
credential := common.NewCredential(
"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)
// 实例化一个请求对象,每个接口都会对应一个request对象
request := lkeap.NewReconstructDocumentSSERequest()
request.FileType = common.StringPtr("TXT")
request.FileBase64 = common.StringPtr("data:application/octet-stream;base64,4oCc6L+Z5bCx6LGh6Iqx5LiA5qC344CC5aaC5p6c5L2g54ix5LiK5LqG5LiA5py155Sf6ZW/IArlnKjkuIDpopfmmJ/mmJ/kuIrnmoToirHvvIzpgqPkuYjlpJzpl7TvvIwgCuS9oOeci+edgOWkqeepuuWwseaEn+WIsOeUnOicnOaEiSAK5b+r44CC5omA5pyJ55qE5pif5pif5LiK6YO9CuWlveixoeW8gOedgOiKseOAguKAnQ==")
// 返回的resp是一个ReconstructDocumentSSEResponse的实例,与请求对象对应
response, err := client.ReconstructDocumentSSE(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return
}
if err != nil {
panic(err)
}
// 输出json格式的字符串回包
if response.Response != nil {
// 非流式响应
fmt.Println(response.ToJsonString())
} else {
// 流式响应
for event := range response.Events {
fmt.Println(string(event.Data))
}
}
}
\ No newline at end of file
......@@ -291,22 +291,6 @@ func (kc *KnowledgeClient) QueryRewriteWithSummary(ctx context.Context, userQues
return kc.QueryRewrite(ctx, req)
}
// ReconstructDocumentSSE performs document reconstruction using the Tencent Cloud LKEAP API.
// It takes a text string, encodes it as Base64, and returns the reconstructed content as a slice of ReconstructDocumentSSEResponse structs for events with Progress "100".
func (kc *KnowledgeClient) ReconstructDocumentSSE(ctx context.Context, text string) ([]ReconstructDocumentSSEResponse, error) {
......@@ -431,8 +415,6 @@ func min(a, b int) int {
return b
}
// ReconstructDocumentSSEResponse defines the response structure for the ReconstructDocumentSSE API.
type ReconstructDocumentSSEResponse struct {
RequestId *string `json:"RequestId,omitempty"`
......@@ -444,4 +426,3 @@ type ReconstructDocumentSSEResponse struct {
FailedPages []*int64 `json:"FailedPages,omitempty"`
StatusCode *string `json:"StatusCode,omitempty"`
}
......@@ -113,8 +113,6 @@ func TestKnowledgeClient_QueryRewrite(t *testing.T) {
}
}
// func TestKnowledgeClient_ReconstructDocumentSSE(t *testing.T) {
// // Warning: Do not hardcode credentials in production code. Use environment variables or a secure vault.
// os.Setenv("TENCENTCLOUD_SECRET_ID", "AKID64oLfmfLtESUJ6i8LPSM4gCVbiniQuBF")
......
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