From 9f32fbd99871517279abbdd051c65086331f79d2 Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Sat, 2 Sep 2023 21:17:26 +0800 Subject: [PATCH] Add Model interface --- ai/model.go | 36 ++++++++++++++++ ai/{ai.go => openai.go} | 63 ++++----------------------- ai/{proxy.go => openai_proxy.go} | 0 ai/query.go | 74 ++++++++++++++++++++++++++++++++ ai/{ai_test.go => query_test.go} | 0 controllers/message.go | 11 +++-- go.mod | 2 +- go.sum | 2 +- object/message_ai.go | 24 +++++++++++ object/provider.go | 14 ++++++ web/src/ChatBox.js | 2 +- 11 files changed, 167 insertions(+), 61 deletions(-) create mode 100644 ai/model.go rename ai/{ai.go => openai.go} (55%) rename ai/{proxy.go => openai_proxy.go} (100%) create mode 100644 ai/query.go rename ai/{ai_test.go => query_test.go} (100%) create mode 100644 object/message_ai.go diff --git a/ai/model.go b/ai/model.go new file mode 100644 index 0000000..f7348b5 --- /dev/null +++ b/ai/model.go @@ -0,0 +1,36 @@ +// Copyright 2023 The casbin Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ai + +import ( + "io" + "strings" +) + +type ModelProvider interface { + QueryText(question string, writer io.Writer, builder *strings.Builder) error +} + +func GetModelProvider(typ string, secretKey string) (ModelProvider, error) { + if typ == "OpenAI API - GPT 3.5" { + p, err := NewOpenaiGpt3p5ModelProvider(secretKey) + if err != nil { + return nil, err + } + return p, nil + } + + return nil, nil +} diff --git a/ai/ai.go b/ai/openai.go similarity index 55% rename from ai/ai.go rename to ai/openai.go index 0178a23..63e96cf 100644 --- a/ai/ai.go +++ b/ai/openai.go @@ -20,63 +20,23 @@ import ( "io" "net/http" "strings" - "time" "github.com/sashabaranov/go-openai" ) -func queryAnswer(authToken string, question string, timeout int) (string, error) { - // fmt.Printf("Question: %s\n", question) - - client := getProxyClientFromToken(authToken) - - ctx, cancel := context.WithTimeout(context.Background(), time.Duration(2+timeout*2)*time.Second) - defer cancel() - - resp, err := client.CreateChatCompletion( - ctx, - openai.ChatCompletionRequest{ - Model: openai.GPT3Dot5Turbo, - Messages: []openai.ChatCompletionMessage{ - { - Role: openai.ChatMessageRoleUser, - Content: question, - }, - }, - }, - ) - if err != nil { - return "", err - } - - res := resp.Choices[0].Message.Content - res = strings.Trim(res, "\n") - // fmt.Printf("Answer: %s\n\n", res) - return res, nil +type OpenaiGpt3p5ModelProvider struct { + SecretKey string } -func QueryAnswerSafe(authToken string, question string) string { - var res string - var err error - for i := 0; i < 10; i++ { - res, err = queryAnswer(authToken, question, i) - if err != nil { - if i > 0 { - fmt.Printf("\tFailed (%d): %s\n", i+1, err.Error()) - } - } else { - break - } - } - if err != nil { - panic(err) +func NewOpenaiGpt3p5ModelProvider(secretKey string) (*OpenaiGpt3p5ModelProvider, error) { + p := &OpenaiGpt3p5ModelProvider{ + SecretKey: secretKey, } - - return res + return p, nil } -func QueryAnswerStream(authToken string, question string, writer io.Writer, builder *strings.Builder) error { - client := getProxyClientFromToken(authToken) +func (p *OpenaiGpt3p5ModelProvider) QueryText(question string, writer io.Writer, builder *strings.Builder) error { + client := getProxyClientFromToken(p.SecretKey) ctx := context.Background() flusher, ok := writer.(http.Flusher) @@ -139,10 +99,3 @@ func QueryAnswerStream(authToken string, question string, writer io.Writer, buil return nil } - -func GetQuestionWithKnowledge(knowledge string, question string) string { - return fmt.Sprintf(`paragraph: %s - -You are a reading comprehension expert. Please answer the following questions based on the provided content. The content may be in a different language from the questions, so you need to understand the content according to the language of the questions and ensure that your answers are translated into the same language as the questions: -Q1: %s`, knowledge, question) -} diff --git a/ai/proxy.go b/ai/openai_proxy.go similarity index 100% rename from ai/proxy.go rename to ai/openai_proxy.go diff --git a/ai/query.go b/ai/query.go new file mode 100644 index 0000000..e0840b0 --- /dev/null +++ b/ai/query.go @@ -0,0 +1,74 @@ +// Copyright 2023 The casbin Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ai + +import ( + "context" + "fmt" + "strings" + "time" + + "github.com/sashabaranov/go-openai" +) + +func queryAnswer(authToken string, question string, timeout int) (string, error) { + // fmt.Printf("Question: %s\n", question) + + client := getProxyClientFromToken(authToken) + + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(2+timeout*2)*time.Second) + defer cancel() + + resp, err := client.CreateChatCompletion( + ctx, + openai.ChatCompletionRequest{ + Model: openai.GPT3Dot5Turbo, + Messages: []openai.ChatCompletionMessage{ + { + Role: openai.ChatMessageRoleUser, + Content: question, + }, + }, + }, + ) + if err != nil { + return "", err + } + + res := resp.Choices[0].Message.Content + res = strings.Trim(res, "\n") + // fmt.Printf("Answer: %s\n\n", res) + return res, nil +} + +func QueryAnswerSafe(authToken string, question string) string { + var res string + var err error + for i := 0; i < 10; i++ { + res, err = queryAnswer(authToken, question, i) + if err != nil { + if i > 0 { + fmt.Printf("\tFailed (%d): %s\n", i+1, err.Error()) + } + } else { + break + } + } + if err != nil { + panic(err) + } + + return res +} diff --git a/ai/ai_test.go b/ai/query_test.go similarity index 100% rename from ai/ai_test.go rename to ai/query_test.go diff --git a/controllers/message.go b/controllers/message.go index e474751..574f7e3 100644 --- a/controllers/message.go +++ b/controllers/message.go @@ -19,7 +19,6 @@ import ( "fmt" "strings" - "github.com/casbin/casibase/ai" "github.com/casbin/casibase/object" "github.com/casbin/casibase/util" ) @@ -179,13 +178,19 @@ func (c *ApiController) GetMessageAnswer() { return } - realQuestion := ai.GetQuestionWithKnowledge(nearestText, question) + realQuestion := object.GetRefinedQuestion(nearestText, question) fmt.Printf("Question: [%s]\n", question) fmt.Printf("Context: [%s]\n", nearestText) fmt.Printf("Answer: [") - err = ai.QueryAnswerStream(authToken, realQuestion, c.Ctx.ResponseWriter, &stringBuilder) + modelProvider, err := provider.GetModelProvider() + if err != nil { + c.ResponseErrorStream(err.Error()) + return + } + + err = modelProvider.QueryText(realQuestion, c.Ctx.ResponseWriter, &stringBuilder) if err != nil { c.ResponseErrorStream(err.Error()) return diff --git a/go.mod b/go.mod index e3be8a1..f90e445 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/casbin/casibase go 1.18 require ( + code.sajari.com/docconv v1.3.5 github.com/aliyun/alibaba-cloud-sdk-go v1.61.1585 github.com/aliyun/aliyun-oss-go-sdk v2.2.2+incompatible github.com/astaxie/beego v1.12.3 @@ -26,7 +27,6 @@ require ( ) require ( - code.sajari.com/docconv v1.3.5 // indirect github.com/JalfResi/justext v0.0.0-20170829062021-c0282dea7198 // indirect github.com/PuerkitoBio/goquery v1.5.1 // indirect github.com/advancedlogic/GoOse v0.0.0-20191112112754-e742535969c1 // indirect diff --git a/go.sum b/go.sum index 9602ea9..2c736b2 100644 --- a/go.sum +++ b/go.sum @@ -542,6 +542,7 @@ github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnh github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE= github.com/otiai10/gosseract/v2 v2.2.4 h1:h/PV+oJqke8q2Ccw9bjpMBWfd7N2vtGDCUcihZj3nRo= github.com/otiai10/gosseract/v2 v2.2.4/go.mod h1:ahOp/kHojnOMGv1RaUnR0jwY5JVa6BYKhYAS8nbMLSo= +github.com/otiai10/mint v1.3.0 h1:Ady6MKVezQwHBkGzLFbrsywyp09Ah7rkmfjV3Bcr5uc= github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= @@ -1255,7 +1256,6 @@ modernc.org/token v1.0.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= modernc.org/z v1.0.1/go.mod h1:8/SRk5C/HgiQWCgXdfpb+1RvhORdkz5sw72d3jjtyqA= modernc.org/z v1.5.1 h1:RTNHdsrOpeoSeOF4FbzTo8gBYByaJ5xT7NgZ9ZqRiJM= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= -rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= diff --git a/object/message_ai.go b/object/message_ai.go new file mode 100644 index 0000000..76588dc --- /dev/null +++ b/object/message_ai.go @@ -0,0 +1,24 @@ +// Copyright 2023 The casbin Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package object + +import "fmt" + +func GetRefinedQuestion(knowledge string, question string) string { + return fmt.Sprintf(`paragraph: %s + +You are a reading comprehension expert. Please answer the following questions based on the provided content. The content may be in a different language from the questions, so you need to understand the content according to the language of the questions and ensure that your answers are translated into the same language as the questions: +Q1: %s`, knowledge, question) +} diff --git a/object/provider.go b/object/provider.go index 027119b..989f2e0 100644 --- a/object/provider.go +++ b/object/provider.go @@ -17,6 +17,7 @@ package object import ( "fmt" + "github.com/casbin/casibase/ai" "github.com/casbin/casibase/util" "xorm.io/core" ) @@ -154,3 +155,16 @@ func DeleteProvider(provider *Provider) (bool, error) { func (provider *Provider) GetId() string { return fmt.Sprintf("%s/%s", provider.Owner, provider.Name) } + +func (p *Provider) GetModelProvider() (ai.ModelProvider, error) { + pProvider, err := ai.GetModelProvider(p.Type, p.ClientSecret) + if err != nil { + return nil, err + } + + if pProvider == nil { + return nil, fmt.Errorf("the model provider type: %s is not supported", p.Type) + } + + return pProvider, nil +} diff --git a/web/src/ChatBox.js b/web/src/ChatBox.js index 2326020..caf9189 100644 --- a/web/src/ChatBox.js +++ b/web/src/ChatBox.js @@ -16,7 +16,7 @@ import React from "react"; import {Avatar, ChatContainer, ConversationHeader, MainContainer, Message, MessageInput, MessageList} from "@chatscope/chat-ui-kit-react"; import "@chatscope/chat-ui-kit-styles/dist/default/styles.min.css"; -const robot = "https://cdn.casbin.com/casdoor/resource/built-in/admin/gpt.png"; +const robot = "https://cdn.casbin.org/img/social_openai.svg"; class ChatBox extends React.Component { constructor(props) {