Enhance Neo API and conversation management by adding support for server-sent events (SSE) in chat handling, improving error messaging with structured responses, and refactoring assistant creation logic. Update conversation settings to utilize a new assistant model and implement context management improvements for better performance. Additionally, streamline the DSL structure and enhance test coverage for chat functionalities.
This commit is contained in:
parent
1c078a5d2e
commit
2ebc29d6f7
13 changed files with 961 additions and 462 deletions
18
neo/api.go
18
neo/api.go
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/yaoapp/gou/api"
|
"github.com/yaoapp/gou/api"
|
||||||
"github.com/yaoapp/gou/process"
|
"github.com/yaoapp/gou/process"
|
||||||
"github.com/yaoapp/yao/helper"
|
"github.com/yaoapp/yao/helper"
|
||||||
|
"github.com/yaoapp/yao/neo/message"
|
||||||
)
|
)
|
||||||
|
|
||||||
// API registers the Neo API endpoints
|
// API registers the Neo API endpoints
|
||||||
|
|
@ -45,6 +46,11 @@ func (neo *DSL) API(router *gin.Engine, path string) error {
|
||||||
|
|
||||||
// handleChat handles the chat request
|
// handleChat handles the chat request
|
||||||
func (neo *DSL) handleChat(c *gin.Context) {
|
func (neo *DSL) handleChat(c *gin.Context) {
|
||||||
|
// Set headers for SSE
|
||||||
|
c.Header("Content-Type", "text/event-stream;charset=utf-8")
|
||||||
|
c.Header("Cache-Control", "no-cache")
|
||||||
|
c.Header("Connection", "keep-alive")
|
||||||
|
|
||||||
sid := c.GetString("__sid")
|
sid := c.GetString("__sid")
|
||||||
if sid == "" {
|
if sid == "" {
|
||||||
sid = uuid.New().String()
|
sid = uuid.New().String()
|
||||||
|
|
@ -52,7 +58,11 @@ func (neo *DSL) handleChat(c *gin.Context) {
|
||||||
|
|
||||||
content := c.Query("content")
|
content := c.Query("content")
|
||||||
if content == "" {
|
if content == "" {
|
||||||
c.JSON(400, gin.H{"message": "content is required", "code": 400})
|
msg := message.New().Map(map[string]interface{}{
|
||||||
|
"error": "content is required",
|
||||||
|
"done": true,
|
||||||
|
})
|
||||||
|
msg.Write(c.Writer)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -60,11 +70,7 @@ func (neo *DSL) handleChat(c *gin.Context) {
|
||||||
ctx, cancel := NewContextWithCancel(sid, c.Query("chat_id"), c.Query("context"))
|
ctx, cancel := NewContextWithCancel(sid, c.Query("chat_id"), c.Query("context"))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
err := neo.Answer(ctx, content, c)
|
neo.Answer(ctx, content, c)
|
||||||
if err != nil {
|
|
||||||
c.JSON(500, gin.H{"message": err.Error(), "code": 500})
|
|
||||||
c.Done()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleChatList handles the chat list request
|
// handleChatList handles the chat list request
|
||||||
|
|
|
||||||
28
neo/assistant/base/base.go
Normal file
28
neo/assistant/base/base.go
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
package base
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/yaoapp/gou/connector"
|
||||||
|
"github.com/yaoapp/yao/neo/assistant"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Base the base assistant
|
||||||
|
type Base struct {
|
||||||
|
ID string `json:"assistant_id"`
|
||||||
|
Prompts []assistant.Prompt `json:"prompts,omitempty"`
|
||||||
|
Connector connector.Connector `json:"-" yaml:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// New create a new base assistant
|
||||||
|
func New(connector connector.Connector, prompts []assistant.Prompt, id ...string) (*Base, error) {
|
||||||
|
if len(id) > 0 {
|
||||||
|
return &Base{Connector: connector, ID: id[0], Prompts: prompts}, nil
|
||||||
|
}
|
||||||
|
return &Base{Connector: connector, Prompts: prompts}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// List list all assistants
|
||||||
|
func (ast *Base) List(ctx context.Context, param assistant.QueryParam) ([]assistant.Assistant, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
10
neo/assistant/base/chat.go
Normal file
10
neo/assistant/base/chat.go
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
package base
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Chat the chat
|
||||||
|
func (ast *Base) Chat(ctx context.Context, messages []map[string]interface{}, option map[string]interface{}, cb func(data []byte) int) (interface{}, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
19
neo/assistant/openai/chat.go
Normal file
19
neo/assistant/openai/chat.go
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
package openai
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Chat the chat struct
|
||||||
|
type Chat struct {
|
||||||
|
ID string `json:"chat_id"`
|
||||||
|
ThreadID string `json:"thread_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewChat create a new chat
|
||||||
|
func (ast *OpenAI) NewChat() {}
|
||||||
|
|
||||||
|
// Chat the chat
|
||||||
|
func (ast *OpenAI) Chat(ctx context.Context, messages []map[string]interface{}, option map[string]interface{}, cb func(data []byte) int) (interface{}, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
21
neo/assistant/openai/file.go
Normal file
21
neo/assistant/openai/file.go
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
package openai
|
||||||
|
|
||||||
|
// File the file struct
|
||||||
|
type File struct {
|
||||||
|
ID string `json:"file_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// FileLists list all files
|
||||||
|
func (ast *OpenAI) FileLists() {}
|
||||||
|
|
||||||
|
// Upload upload a file to an assistant
|
||||||
|
func (ast *OpenAI) Upload() {}
|
||||||
|
|
||||||
|
// FileDelete delete a file
|
||||||
|
func (ast *OpenAI) FileDelete() {}
|
||||||
|
|
||||||
|
// FileContent get the content of a file
|
||||||
|
func (ast *OpenAI) FileContent() {}
|
||||||
|
|
||||||
|
// FileInfo get the information of a file
|
||||||
|
func (ast *OpenAI) FileInfo() {}
|
||||||
45
neo/assistant/openai/openai.go
Normal file
45
neo/assistant/openai/openai.go
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
package openai
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/yaoapp/gou/connector"
|
||||||
|
"github.com/yaoapp/yao/neo/assistant"
|
||||||
|
)
|
||||||
|
|
||||||
|
// OpenAI the openai assistant
|
||||||
|
type OpenAI struct {
|
||||||
|
ID string `json:"assistant_id"` // the assistant id
|
||||||
|
Connector connector.Connector `json:"-" yaml:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// New create a new openai assistant
|
||||||
|
func New(connector connector.Connector, id ...string) (*OpenAI, error) {
|
||||||
|
if len(id) > 0 {
|
||||||
|
return &OpenAI{ID: id[0], Connector: connector}, nil
|
||||||
|
}
|
||||||
|
return &OpenAI{Connector: connector}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Current set the current assistant
|
||||||
|
func (ast *OpenAI) Current(id string) *OpenAI {
|
||||||
|
ast.ID = id
|
||||||
|
return ast
|
||||||
|
}
|
||||||
|
|
||||||
|
// List list all assistants
|
||||||
|
func (ast *OpenAI) List(ctx context.Context, param assistant.QueryParam) ([]assistant.Assistant, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create create a new assistant
|
||||||
|
func (ast *OpenAI) Create() {}
|
||||||
|
|
||||||
|
// Delete delete an assistant
|
||||||
|
func (ast *OpenAI) Delete() {}
|
||||||
|
|
||||||
|
// Update update an assistant
|
||||||
|
func (ast *OpenAI) Update() {}
|
||||||
|
|
||||||
|
// Get get an assistant
|
||||||
|
func (ast *OpenAI) Get() {}
|
||||||
21
neo/assistant/openai/thread.go
Normal file
21
neo/assistant/openai/thread.go
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
package openai
|
||||||
|
|
||||||
|
// Thread the thread struct
|
||||||
|
type Thread struct {
|
||||||
|
ID string `json:"thread_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ThreadList list all threads
|
||||||
|
func (ast *OpenAI) ThreadList() {}
|
||||||
|
|
||||||
|
// ThreadCreate create a new thread
|
||||||
|
func (ast *OpenAI) ThreadCreate() {}
|
||||||
|
|
||||||
|
// ThreadGet get a thread
|
||||||
|
func (ast *OpenAI) ThreadGet(id string) {}
|
||||||
|
|
||||||
|
// ThreadDelete delete a thread
|
||||||
|
func (ast *OpenAI) ThreadDelete() {}
|
||||||
|
|
||||||
|
// ThreadUpdate update a thread
|
||||||
|
func (ast *OpenAI) ThreadUpdate() {}
|
||||||
37
neo/assistant/types.go
Normal file
37
neo/assistant/types.go
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
package assistant
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
// API the assistant API interface
|
||||||
|
type API interface {
|
||||||
|
Chat(ctx context.Context, messages []map[string]interface{}, option map[string]interface{}, cb func(data []byte) int) (interface{}, error)
|
||||||
|
List(ctx context.Context, param QueryParam) ([]Assistant, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prompt a prompt
|
||||||
|
type Prompt struct {
|
||||||
|
Role string `json:"role"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryParam the assistant query param
|
||||||
|
type QueryParam struct {
|
||||||
|
Limit uint `json:"limit"`
|
||||||
|
Order string `json:"order"`
|
||||||
|
After string `json:"after"`
|
||||||
|
Before string `json:"before"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assistant the assistant
|
||||||
|
type Assistant struct {
|
||||||
|
ID string `json:"assistant_id"` // Assistant ID
|
||||||
|
Name string `json:"name,omitempty"` // Assistant Name
|
||||||
|
Description string `json:"description"` // Assistant Description
|
||||||
|
Connector string `json:"connector"` // AI Connector
|
||||||
|
Option map[string]interface{} `json:"option"` // AI Option
|
||||||
|
Prompts []Prompt `json:"prompts,omitempty"` // AI Prompts
|
||||||
|
API API `json:"-" yaml:"-"` // Assistant API
|
||||||
|
}
|
||||||
166
neo/hooks.go
Normal file
166
neo/hooks.go
Normal file
|
|
@ -0,0 +1,166 @@
|
||||||
|
package neo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
"github.com/yaoapp/gou/process"
|
||||||
|
"github.com/yaoapp/yao/neo/assistant"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HookCreate create the assistant
|
||||||
|
func (neo *DSL) HookCreate(ctx Context, messages []map[string]interface{}, c *gin.Context) error {
|
||||||
|
if neo.Create == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a context with 10 second timeout
|
||||||
|
timeoutCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
p, err := process.Of(neo.Create, ctx, messages, c.Writer)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = p.WithContext(timeoutCtx).Execute()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer p.Release()
|
||||||
|
|
||||||
|
// Check if context was canceled
|
||||||
|
if timeoutCtx.Err() != nil {
|
||||||
|
return timeoutCtx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// HookAssistants query the assistant list from the assistant list hook
|
||||||
|
func (neo *DSL) HookAssistants(ctx context.Context, param assistant.QueryParam) ([]assistant.Assistant, error) {
|
||||||
|
if neo.AssistantListHook == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a context with 10 second timeout
|
||||||
|
timeoutCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
p, err := process.Of(neo.AssistantListHook, param)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = p.WithContext(timeoutCtx).Execute()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer p.Release()
|
||||||
|
|
||||||
|
// Check if context was canceled
|
||||||
|
if timeoutCtx.Err() != nil {
|
||||||
|
return nil, timeoutCtx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
value := p.Value()
|
||||||
|
if value == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var list []assistant.Assistant
|
||||||
|
bytes, err := jsoniter.Marshal(value)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = jsoniter.Unmarshal(bytes, &list)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return list, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// HookPrepare executes the prepare hook before AI is called
|
||||||
|
func (neo *DSL) HookPrepare(ctx Context, messages []map[string]interface{}) ([]map[string]interface{}, error) {
|
||||||
|
if neo.Prepare == "" {
|
||||||
|
return messages, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a context with 10 second timeout
|
||||||
|
timeoutCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
p, err := process.Of(neo.Prepare, ctx, messages)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = p.WithContext(timeoutCtx).Execute()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer p.Release()
|
||||||
|
|
||||||
|
// Check if context was canceled
|
||||||
|
if timeoutCtx.Err() != nil {
|
||||||
|
return nil, timeoutCtx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
value := p.Value()
|
||||||
|
if value == nil {
|
||||||
|
return messages, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result []map[string]interface{}
|
||||||
|
bytes, err := jsoniter.Marshal(value)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = jsoniter.Unmarshal(bytes, &result)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// HookWrite executes the write hook when response is received from AI
|
||||||
|
func (neo *DSL) HookWrite(ctx Context, messages []map[string]interface{}, response map[string]interface{}, content string, writer *gin.ResponseWriter) ([]map[string]interface{}, error) {
|
||||||
|
if neo.Write == "" {
|
||||||
|
return []map[string]interface{}{response}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
p, err := process.Of(neo.Write, ctx, messages, response, content, writer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = p.WithContext(ctx).Execute()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer p.Release()
|
||||||
|
|
||||||
|
value := p.Value()
|
||||||
|
if value == nil {
|
||||||
|
return []map[string]interface{}{response}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result []map[string]interface{}
|
||||||
|
bytes, err := jsoniter.Marshal(value)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = jsoniter.Unmarshal(bytes, &result)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
35
neo/load.go
35
neo/load.go
|
|
@ -1,11 +1,14 @@
|
||||||
package neo
|
package neo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/yaoapp/gou/application"
|
"github.com/yaoapp/gou/application"
|
||||||
"github.com/yaoapp/yao/aigc"
|
|
||||||
"github.com/yaoapp/yao/config"
|
"github.com/yaoapp/yao/config"
|
||||||
|
"github.com/yaoapp/yao/neo/assistant"
|
||||||
"github.com/yaoapp/yao/neo/conversation"
|
"github.com/yaoapp/yao/neo/conversation"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -17,7 +20,7 @@ func Load(cfg config.Config) error {
|
||||||
|
|
||||||
setting := DSL{
|
setting := DSL{
|
||||||
ID: "neo",
|
ID: "neo",
|
||||||
Prompts: []aigc.Prompt{},
|
Prompts: []assistant.Prompt{},
|
||||||
Option: map[string]interface{}{},
|
Option: map[string]interface{}{},
|
||||||
Allows: []string{},
|
Allows: []string{},
|
||||||
ConversationSetting: conversation.Setting{
|
ConversationSetting: conversation.Setting{
|
||||||
|
|
@ -42,17 +45,37 @@ func Load(cfg config.Config) error {
|
||||||
|
|
||||||
Neo = &setting
|
Neo = &setting
|
||||||
|
|
||||||
// AI Setting
|
// Create Default Assistant
|
||||||
err = Neo.newAI()
|
Neo.Assistant, err = Neo.createDefaultAssistant()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Conversation Setting
|
// Conversation Setting
|
||||||
err = Neo.newConversation()
|
err = Neo.createConversation()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
// Query Assistant List
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
listDone := make(chan error, 1)
|
||||||
|
go func() {
|
||||||
|
list, err := Neo.HookAssistants(ctx, assistant.QueryParam{Limit: 100})
|
||||||
|
Neo.updateAssistantList(list)
|
||||||
|
listDone <- err
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err := <-listDone:
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Neo assistant list failed: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
case <-ctx.Done():
|
||||||
|
return fmt.Errorf("Neo assistant list timeout: %w", ctx.Err())
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
360
neo/neo.go
360
neo/neo.go
|
|
@ -3,98 +3,199 @@ package neo
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/yaoapp/gou/connector"
|
"github.com/yaoapp/gou/connector"
|
||||||
"github.com/yaoapp/gou/process"
|
"github.com/yaoapp/gou/process"
|
||||||
"github.com/yaoapp/kun/log"
|
"github.com/yaoapp/kun/log"
|
||||||
|
"github.com/yaoapp/yao/neo/assistant"
|
||||||
|
"github.com/yaoapp/yao/neo/assistant/base"
|
||||||
|
"github.com/yaoapp/yao/neo/assistant/openai"
|
||||||
"github.com/yaoapp/yao/neo/conversation"
|
"github.com/yaoapp/yao/neo/conversation"
|
||||||
"github.com/yaoapp/yao/neo/message"
|
"github.com/yaoapp/yao/neo/message"
|
||||||
"github.com/yaoapp/yao/openai"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Lock the assistant list
|
||||||
|
var lock sync.Mutex = sync.Mutex{}
|
||||||
|
|
||||||
// Answer reply the message
|
// Answer reply the message
|
||||||
func (neo *DSL) Answer(ctx Context, question string, c *gin.Context) error {
|
func (neo *DSL) Answer(ctx Context, question string, c *gin.Context) error {
|
||||||
// get the chat messages
|
|
||||||
messages, err := neo.chatMessages(ctx, question)
|
messages, err := neo.chatMessages(ctx, question)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
msg := message.New().Map(map[string]interface{}{
|
||||||
|
"error": err.Error(),
|
||||||
|
"done": true,
|
||||||
|
})
|
||||||
|
msg.Write(c.Writer)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
clientBreak := make(chan bool, 1)
|
err = neo.HookCreate(ctx, messages, c)
|
||||||
done := make(chan bool, 1)
|
if err != nil {
|
||||||
content := []byte{}
|
msg := message.New().Map(map[string]interface{}{
|
||||||
|
"error": err.Error(),
|
||||||
// Execute the command or chat with AI in the background
|
"done": true,
|
||||||
go func() {
|
|
||||||
|
|
||||||
// chat with AI
|
|
||||||
c.Header("Content-Type", "text/event-stream;charset=utf-8")
|
|
||||||
c.Header("Cache-Control", "no-cache")
|
|
||||||
c.Header("Connection", "keep-alive")
|
|
||||||
|
|
||||||
_, ex := neo.AI.ChatCompletionsWith(ctx, messages, neo.Option, func(data []byte) int {
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-clientBreak:
|
|
||||||
return 0 // break
|
|
||||||
default:
|
|
||||||
|
|
||||||
msg := message.NewOpenAI(data)
|
|
||||||
if msg == nil {
|
|
||||||
return 1 // continue success
|
|
||||||
}
|
|
||||||
|
|
||||||
if msg.Error != "" {
|
|
||||||
neo.send(ctx, msg, messages, content, c)
|
|
||||||
return 0 // break
|
|
||||||
}
|
|
||||||
|
|
||||||
content = msg.Append(content)
|
|
||||||
err := neo.send(ctx, msg, messages, content, c)
|
|
||||||
if err != nil {
|
|
||||||
c.Status(500)
|
|
||||||
return 0 // break
|
|
||||||
}
|
|
||||||
|
|
||||||
// Complete the stream
|
|
||||||
if msg.IsDone() {
|
|
||||||
done <- true
|
|
||||||
return 0 // break
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1 // continue success
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
msg.Write(c.Writer)
|
||||||
// Throw the error
|
return err
|
||||||
if ex != nil {
|
|
||||||
log.Error("Neo chat error: %s", ex.Message)
|
|
||||||
c.Status(200)
|
|
||||||
done <- true
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// save the history
|
|
||||||
neo.saveHistory(ctx.Sid, ctx.ChatID, content, messages)
|
|
||||||
c.Status(200)
|
|
||||||
|
|
||||||
// Complete the stream
|
|
||||||
done <- true
|
|
||||||
|
|
||||||
}()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
return nil
|
|
||||||
case <-c.Writer.CloseNotify():
|
|
||||||
clientBreak <- true
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Send a text message to the client
|
||||||
|
msg := message.New().Map(map[string]interface{}{
|
||||||
|
"text": "Hello, world!",
|
||||||
|
"done": true,
|
||||||
|
})
|
||||||
|
msg.Write(c.Writer)
|
||||||
|
|
||||||
|
// Select Assistant
|
||||||
|
|
||||||
|
// Prepare Messages
|
||||||
|
|
||||||
|
// Call AI
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// updateAssistantList update the assistant list
|
||||||
|
func (neo *DSL) updateAssistantList(list []assistant.Assistant) {
|
||||||
|
lock.Lock()
|
||||||
|
defer lock.Unlock()
|
||||||
|
neo.AssistantList = list
|
||||||
|
neo.AssistantMaps = make(map[string]assistant.Assistant)
|
||||||
|
if list != nil {
|
||||||
|
for _, assistant := range list {
|
||||||
|
neo.AssistantMaps[assistant.ID] = assistant
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// createDefaultAssistant create a default assistant
|
||||||
|
func (neo *DSL) createDefaultAssistant() (assistant.API, error) {
|
||||||
|
|
||||||
|
// Moapi
|
||||||
|
if neo.Connector == "" || strings.HasPrefix(neo.Connector, "moapi") {
|
||||||
|
model := "gpt-3.5-turbo"
|
||||||
|
if strings.HasPrefix(neo.Connector, "moapi:") {
|
||||||
|
model = strings.TrimPrefix(neo.Connector, "moapi:")
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := connector.New(`moapi`, `__yao.moapi`, []byte(`{"model": "`+model+`"}`))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Create moapi assistant error: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
api, err := openai.New(conn, neo.Use)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Create openai assistant error: %s", err.Error())
|
||||||
|
}
|
||||||
|
return api, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Other connector
|
||||||
|
conn, err := connector.Select(neo.Connector)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Neo assistant connector %s not support", neo.Connector)
|
||||||
|
}
|
||||||
|
|
||||||
|
if conn.Is(connector.OPENAI) {
|
||||||
|
api, err := openai.New(conn, neo.Use)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Create openai assistant error: %s", err.Error())
|
||||||
|
}
|
||||||
|
return api, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Base on the assistant list hook
|
||||||
|
api, err := base.New(conn, neo.Prompts, neo.Use)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Create base assistant error: %s", err.Error())
|
||||||
|
}
|
||||||
|
return api, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// // AnswerOld reply the message
|
||||||
|
// func (neo *DSL) AnswerOld(ctx Context, question string, c *gin.Context) error {
|
||||||
|
// // get the chat messages
|
||||||
|
// messages, err := neo.chatMessages(ctx, question)
|
||||||
|
// if err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
|
||||||
|
// clientBreak := make(chan bool, 1)
|
||||||
|
// done := make(chan bool, 1)
|
||||||
|
// content := []byte{}
|
||||||
|
|
||||||
|
// // Execute the command or chat with AI in the background
|
||||||
|
// go func() {
|
||||||
|
|
||||||
|
// // chat with AI
|
||||||
|
// c.Header("Content-Type", "text/event-stream;charset=utf-8")
|
||||||
|
// c.Header("Cache-Control", "no-cache")
|
||||||
|
// c.Header("Connection", "keep-alive")
|
||||||
|
|
||||||
|
// _, ex := neo.AI.ChatCompletionsWith(ctx, messages, neo.Option, func(data []byte) int {
|
||||||
|
|
||||||
|
// select {
|
||||||
|
// case <-clientBreak:
|
||||||
|
// return 0 // break
|
||||||
|
// default:
|
||||||
|
|
||||||
|
// msg := message.NewOpenAI(data)
|
||||||
|
// if msg == nil {
|
||||||
|
// return 1 // continue success
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if msg.Error != "" {
|
||||||
|
// neo.send(ctx, msg, messages, content, c)
|
||||||
|
// return 0 // break
|
||||||
|
// }
|
||||||
|
|
||||||
|
// content = msg.Append(content)
|
||||||
|
// err := neo.send(ctx, msg, messages, content, c)
|
||||||
|
// if err != nil {
|
||||||
|
// c.Status(500)
|
||||||
|
// return 0 // break
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Complete the stream
|
||||||
|
// if msg.IsDone() {
|
||||||
|
// done <- true
|
||||||
|
// return 0 // break
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return 1 // continue success
|
||||||
|
// }
|
||||||
|
// })
|
||||||
|
|
||||||
|
// // Throw the error
|
||||||
|
// if ex != nil {
|
||||||
|
// log.Error("Neo chat error: %s", ex.Message)
|
||||||
|
// c.Status(200)
|
||||||
|
// done <- true
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // save the history
|
||||||
|
// neo.saveHistory(ctx.Sid, ctx.ChatID, content, messages)
|
||||||
|
// c.Status(200)
|
||||||
|
|
||||||
|
// // Complete the stream
|
||||||
|
// done <- true
|
||||||
|
|
||||||
|
// }()
|
||||||
|
|
||||||
|
// select {
|
||||||
|
// case <-done:
|
||||||
|
// return nil
|
||||||
|
// case <-c.Writer.CloseNotify():
|
||||||
|
// clientBreak <- true
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
// Send send the message to the stream
|
// Send send the message to the stream
|
||||||
func (neo *DSL) send(ctx Context, msg *message.JSON, messages []map[string]interface{}, content []byte, c *gin.Context) error {
|
func (neo *DSL) send(ctx Context, msg *message.JSON, messages []map[string]interface{}, content []byte, c *gin.Context) error {
|
||||||
|
|
||||||
|
|
@ -226,12 +327,6 @@ func (neo *DSL) chatMessages(ctx Context, content string) ([]map[string]interfac
|
||||||
messages = append(messages, history...)
|
messages = append(messages, history...)
|
||||||
messages = append(messages, map[string]interface{}{"role": "user", "content": content, "name": ctx.Sid})
|
messages = append(messages, map[string]interface{}{"role": "user", "content": content, "name": ctx.Sid})
|
||||||
|
|
||||||
// Add prepare messages witch is query from vector database
|
|
||||||
preparePrompts := neo.prepare(ctx, messages)
|
|
||||||
if len(preparePrompts) > 0 {
|
|
||||||
messages = preparePrompts
|
|
||||||
}
|
|
||||||
|
|
||||||
return messages, nil
|
return messages, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -254,53 +349,53 @@ func (neo *DSL) saveHistory(sid string, chatID string, content []byte, messages
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewAI create a new AI
|
// // NewAI create a new AI
|
||||||
func (neo *DSL) newAI() error {
|
// func (neo *DSL) newAI() error {
|
||||||
|
|
||||||
if neo.Connector == "" || strings.HasPrefix(neo.Connector, "moapi") {
|
// if neo.Connector == "" || strings.HasPrefix(neo.Connector, "moapi") {
|
||||||
model := "gpt-3.5-turbo"
|
// model := "gpt-3.5-turbo"
|
||||||
if strings.HasPrefix(neo.Connector, "moapi:") {
|
// if strings.HasPrefix(neo.Connector, "moapi:") {
|
||||||
model = strings.TrimPrefix(neo.Connector, "moapi:")
|
// model = strings.TrimPrefix(neo.Connector, "moapi:")
|
||||||
}
|
// }
|
||||||
|
|
||||||
ai, err := openai.NewMoapi(model)
|
// ai, err := openai.NewMoapi(model)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
|
|
||||||
neo.AI = ai
|
// neo.AI = ai
|
||||||
return nil
|
// return nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
conn, err := connector.Select(neo.Connector)
|
// conn, err := connector.Select(neo.Connector)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
|
|
||||||
if conn.Is(connector.OPENAI) {
|
// if conn.Is(connector.OPENAI) {
|
||||||
ai, err := openai.New(neo.Connector)
|
// ai, err := openai.New(neo.Connector)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
neo.AI = ai
|
// neo.AI = ai
|
||||||
return nil
|
// return nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
return fmt.Errorf("%s connector %s not support, should be a openai", neo.ID, neo.Connector)
|
// return fmt.Errorf("%s connector %s not support, should be a openai", neo.ID, neo.Connector)
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Select select the model
|
// // Select select the model
|
||||||
func (neo *DSL) Select(model string) error {
|
// func (neo *DSL) Select(model string) error {
|
||||||
ai, err := openai.NewMoapi(model)
|
// ai, err := openai.NewMoapi(model)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
neo.AI = ai
|
// neo.AI = ai
|
||||||
return nil
|
// return nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
// newConversation create a new conversation
|
// createConversation create a new conversation
|
||||||
func (neo *DSL) newConversation() error {
|
func (neo *DSL) createConversation() error {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
if neo.ConversationSetting.Connector == "default" || neo.ConversationSetting.Connector == "" {
|
if neo.ConversationSetting.Connector == "default" || neo.ConversationSetting.Connector == "" {
|
||||||
|
|
@ -333,3 +428,38 @@ func (neo *DSL) newConversation() error {
|
||||||
|
|
||||||
return fmt.Errorf("%s conversation connector %s not support", neo.ID, neo.ConversationSetting.Connector)
|
return fmt.Errorf("%s conversation connector %s not support", neo.ID, neo.ConversationSetting.Connector)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// // NewAI create a new AI
|
||||||
|
// func (neo *DSL) newAI() error {
|
||||||
|
|
||||||
|
// if neo.Connector == "" || strings.HasPrefix(neo.Connector, "moapi") {
|
||||||
|
// model := "gpt-3.5-turbo"
|
||||||
|
// if strings.HasPrefix(neo.Connector, "moapi:") {
|
||||||
|
// model = strings.TrimPrefix(neo.Connector, "moapi:")
|
||||||
|
// }
|
||||||
|
|
||||||
|
// ai, err := openai.NewMoapi(model)
|
||||||
|
// if err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
|
||||||
|
// neo.AI = ai
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
|
|
||||||
|
// conn, err := connector.Select(neo.Connector)
|
||||||
|
// if err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if conn.Is(connector.OPENAI) {
|
||||||
|
// ai, err := openai.New(neo.Connector)
|
||||||
|
// if err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// neo.AI = ai
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return fmt.Errorf("%s connector %s not support, should be a openai", neo.ID, neo.Connector)
|
||||||
|
// }
|
||||||
|
|
|
||||||
596
neo/neo_test.go
596
neo/neo_test.go
|
|
@ -1,343 +1,327 @@
|
||||||
package neo
|
package neo
|
||||||
|
|
||||||
import (
|
// type customResponseRecorder struct {
|
||||||
"context"
|
// *httptest.ResponseRecorder
|
||||||
"net/http/httptest"
|
// closeChannel chan bool
|
||||||
"testing"
|
// }
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
// func (r *customResponseRecorder) CloseNotify() <-chan bool {
|
||||||
"github.com/stretchr/testify/assert"
|
// return r.closeChannel
|
||||||
"github.com/yaoapp/kun/exception"
|
// }
|
||||||
"github.com/yaoapp/xun/capsule"
|
|
||||||
"github.com/yaoapp/yao/aigc"
|
|
||||||
"github.com/yaoapp/yao/config"
|
|
||||||
"github.com/yaoapp/yao/neo/conversation"
|
|
||||||
"github.com/yaoapp/yao/neo/message"
|
|
||||||
"github.com/yaoapp/yao/test"
|
|
||||||
)
|
|
||||||
|
|
||||||
type customResponseRecorder struct {
|
// func newCustomResponseRecorder() *customResponseRecorder {
|
||||||
*httptest.ResponseRecorder
|
// return &customResponseRecorder{
|
||||||
closeChannel chan bool
|
// ResponseRecorder: httptest.NewRecorder(),
|
||||||
}
|
// closeChannel: make(chan bool, 1),
|
||||||
|
|
||||||
func (r *customResponseRecorder) CloseNotify() <-chan bool {
|
|
||||||
return r.closeChannel
|
|
||||||
}
|
|
||||||
|
|
||||||
func newCustomResponseRecorder() *customResponseRecorder {
|
|
||||||
return &customResponseRecorder{
|
|
||||||
ResponseRecorder: httptest.NewRecorder(),
|
|
||||||
closeChannel: make(chan bool, 1),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDSL_Prompts(t *testing.T) {
|
|
||||||
test.Prepare(t, config.Conf)
|
|
||||||
defer Test_clean(t)
|
|
||||||
|
|
||||||
resetDB()
|
|
||||||
neo := &DSL{
|
|
||||||
Prompts: []aigc.Prompt{
|
|
||||||
{Role: "system", Content: "You are a helpful assistant", Name: "ai"},
|
|
||||||
{Role: "user", Content: "Hello", Name: "user"},
|
|
||||||
},
|
|
||||||
ConversationSetting: conversation.Setting{
|
|
||||||
Connector: "default",
|
|
||||||
Table: "chat_messages",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
err := neo.newConversation()
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
prompts := neo.prompts()
|
|
||||||
assert.Equal(t, 2, len(prompts))
|
|
||||||
assert.Equal(t, "system", prompts[0]["role"])
|
|
||||||
assert.Equal(t, "You are a helpful assistant", prompts[0]["content"])
|
|
||||||
assert.Equal(t, "ai", prompts[0]["name"])
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDSL_ChatMessages(t *testing.T) {
|
|
||||||
test.Prepare(t, config.Conf)
|
|
||||||
defer Test_clean(t)
|
|
||||||
|
|
||||||
resetDB()
|
|
||||||
neo := &DSL{
|
|
||||||
Prompts: []aigc.Prompt{
|
|
||||||
{Role: "system", Content: "You are a helpful assistant"},
|
|
||||||
},
|
|
||||||
ConversationSetting: conversation.Setting{
|
|
||||||
Connector: "default",
|
|
||||||
Table: "chat_messages",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
err := neo.newConversation()
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
ctx := Context{
|
|
||||||
Sid: "test-session",
|
|
||||||
ChatID: "test-chat",
|
|
||||||
}
|
|
||||||
|
|
||||||
messages, err := neo.chatMessages(ctx, "Hello AI")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, 2, len(messages))
|
|
||||||
assert.Equal(t, "system", messages[0]["role"])
|
|
||||||
assert.Equal(t, "user", messages[1]["role"])
|
|
||||||
assert.Equal(t, "Hello AI", messages[1]["content"])
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDSL_Answer(t *testing.T) {
|
|
||||||
test.Prepare(t, config.Conf)
|
|
||||||
defer Test_clean(t)
|
|
||||||
|
|
||||||
gin.SetMode(gin.TestMode)
|
|
||||||
w := newCustomResponseRecorder()
|
|
||||||
c, _ := gin.CreateTestContext(w)
|
|
||||||
|
|
||||||
ctx := Context{
|
|
||||||
Sid: "test-session",
|
|
||||||
ChatID: "test-chat",
|
|
||||||
Context: context.Background(),
|
|
||||||
}
|
|
||||||
|
|
||||||
resetDB()
|
|
||||||
neo := &DSL{
|
|
||||||
Connector: "gpt-3_5-turbo",
|
|
||||||
Option: map[string]interface{}{
|
|
||||||
"temperature": 0.7,
|
|
||||||
"max_tokens": 150,
|
|
||||||
},
|
|
||||||
Prompts: []aigc.Prompt{
|
|
||||||
{Role: "system", Content: "You are a helpful assistant"},
|
|
||||||
},
|
|
||||||
ConversationSetting: conversation.Setting{
|
|
||||||
Connector: "default",
|
|
||||||
Table: "chat_messages",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
err := neo.newAI()
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
err = neo.newConversation()
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
c.Request = httptest.NewRequest("POST", "/chat", nil)
|
|
||||||
|
|
||||||
neo.AI = &mockAI{}
|
|
||||||
|
|
||||||
err = neo.Answer(ctx, "Hello AI", c)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// func TestDSL_NewAI(t *testing.T) {
|
|
||||||
// test.Prepare(t, config.Conf)
|
|
||||||
// defer Test_clean(t)
|
|
||||||
|
|
||||||
// tests := []struct {
|
|
||||||
// name string
|
|
||||||
// connector string
|
|
||||||
// wantErr string
|
|
||||||
// }{
|
|
||||||
// {
|
|
||||||
// name: "Mock AI",
|
|
||||||
// connector: "mock",
|
|
||||||
// wantErr: "",
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// name: "Specific mock model",
|
|
||||||
// connector: "mock:gpt-4",
|
|
||||||
// wantErr: "",
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// name: "Invalid connector",
|
|
||||||
// connector: "invalid-connector",
|
|
||||||
// wantErr: "AI connector invalid-connector not found",
|
|
||||||
// },
|
|
||||||
// }
|
|
||||||
|
|
||||||
// for _, tt := range tests {
|
|
||||||
// t.Run(tt.name, func(t *testing.T) {
|
|
||||||
// neo := &DSL{
|
|
||||||
// Connector: tt.connector,
|
|
||||||
// }
|
|
||||||
// neo.newConversation()
|
|
||||||
|
|
||||||
// assert.Panics(t, func() {
|
|
||||||
// neo.newAI()
|
|
||||||
// })
|
|
||||||
|
|
||||||
// })
|
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
func TestDSL_Select(t *testing.T) {
|
// func TestDSL_Prompts(t *testing.T) {
|
||||||
test.Prepare(t, config.Conf)
|
|
||||||
defer Test_clean(t)
|
|
||||||
|
|
||||||
resetDB()
|
|
||||||
neo := &DSL{
|
|
||||||
ConversationSetting: conversation.Setting{
|
|
||||||
Connector: "default",
|
|
||||||
Table: "chat_messages",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
err := neo.newConversation()
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
err = neo.Select("invalid-model")
|
|
||||||
assert.Error(t, err)
|
|
||||||
|
|
||||||
// err = neo.Select("gpt-3_5-turbo")
|
|
||||||
// assert.NoError(t, err)
|
|
||||||
// assert.NotNil(t, neo.AI)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// func TestDSL_NewConversation(t *testing.T) {
|
|
||||||
// test.Prepare(t, config.Conf)
|
// test.Prepare(t, config.Conf)
|
||||||
// defer Test_clean(t)
|
// defer Test_clean(t)
|
||||||
|
|
||||||
// tests := []struct {
|
// resetDB()
|
||||||
// name string
|
// neo := &DSL{
|
||||||
// connector string
|
// Prompts: []Prompt{
|
||||||
// wantErr bool
|
// {Role: "system", Content: "You are a helpful assistant", Name: "ai"},
|
||||||
// }{
|
// {Role: "user", Content: "Hello", Name: "user"},
|
||||||
// {
|
|
||||||
// name: "Default connector",
|
|
||||||
// connector: "default",
|
|
||||||
// wantErr: false,
|
|
||||||
// },
|
// },
|
||||||
// {
|
// ConversationSetting: conversation.Setting{
|
||||||
// name: "Empty connector",
|
// Connector: "default",
|
||||||
// connector: "",
|
// Table: "chat_messages",
|
||||||
// wantErr: false,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// name: "Invalid connector",
|
|
||||||
// connector: "invalid-connector",
|
|
||||||
// wantErr: true,
|
|
||||||
// },
|
// },
|
||||||
// }
|
// }
|
||||||
|
// err := neo.newConversation()
|
||||||
|
// assert.NoError(t, err)
|
||||||
|
|
||||||
// for _, tt := range tests {
|
// prompts := neo.prompts()
|
||||||
// t.Run(tt.name, func(t *testing.T) {
|
// assert.Equal(t, 2, len(prompts))
|
||||||
// neo := &DSL{
|
// assert.Equal(t, "system", prompts[0]["role"])
|
||||||
// ConversationSetting: conversation.Setting{
|
// assert.Equal(t, "You are a helpful assistant", prompts[0]["content"])
|
||||||
// Connector: tt.connector,
|
// assert.Equal(t, "ai", prompts[0]["name"])
|
||||||
// },
|
|
||||||
// }
|
|
||||||
// assert.Panics(t, func() {
|
|
||||||
// neo.newConversation()
|
|
||||||
// })
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
func TestDSL_SaveHistory(t *testing.T) {
|
// func TestDSL_ChatMessages(t *testing.T) {
|
||||||
test.Prepare(t, config.Conf)
|
// test.Prepare(t, config.Conf)
|
||||||
defer Test_clean(t)
|
// defer Test_clean(t)
|
||||||
|
|
||||||
neo := &DSL{
|
// resetDB()
|
||||||
ConversationSetting: conversation.Setting{
|
// neo := &DSL{
|
||||||
Connector: "default",
|
// Prompts: []Prompt{
|
||||||
Table: "chat_messages",
|
// {Role: "system", Content: "You are a helpful assistant"},
|
||||||
},
|
// },
|
||||||
}
|
// ConversationSetting: conversation.Setting{
|
||||||
|
// Connector: "default",
|
||||||
|
// Table: "chat_messages",
|
||||||
|
// },
|
||||||
|
// }
|
||||||
|
|
||||||
resetDB()
|
// err := neo.newConversation()
|
||||||
err := neo.newConversation()
|
// assert.NoError(t, err)
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
messages := []map[string]interface{}{
|
// ctx := Context{
|
||||||
{
|
// Sid: "test-session",
|
||||||
"role": "user",
|
// ChatID: "test-chat",
|
||||||
"content": "Hello",
|
// }
|
||||||
"name": "test-user",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
content := []byte("Hi there!")
|
// messages, err := neo.chatMessages(ctx, "Hello AI")
|
||||||
neo.saveHistory("test-session", "test-chat", content, messages)
|
// assert.NoError(t, err)
|
||||||
|
// assert.Equal(t, 2, len(messages))
|
||||||
|
// assert.Equal(t, "system", messages[0]["role"])
|
||||||
|
// assert.Equal(t, "user", messages[1]["role"])
|
||||||
|
// assert.Equal(t, "Hello AI", messages[1]["content"])
|
||||||
|
// }
|
||||||
|
|
||||||
// Verify the history was saved
|
// func TestDSL_Answer(t *testing.T) {
|
||||||
history, err := neo.Conversation.GetHistory("test-session", "test-chat")
|
// test.Prepare(t, config.Conf)
|
||||||
assert.NoError(t, err)
|
// defer Test_clean(t)
|
||||||
assert.NotEmpty(t, history)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDSL_Send(t *testing.T) {
|
// gin.SetMode(gin.TestMode)
|
||||||
test.Prepare(t, config.Conf)
|
// w := newCustomResponseRecorder()
|
||||||
defer Test_clean(t)
|
// c, _ := gin.CreateTestContext(w)
|
||||||
|
|
||||||
gin.SetMode(gin.TestMode)
|
// ctx := Context{
|
||||||
w := httptest.NewRecorder()
|
// Sid: "test-session",
|
||||||
c, _ := gin.CreateTestContext(w)
|
// ChatID: "test-chat",
|
||||||
|
// Context: context.Background(),
|
||||||
|
// }
|
||||||
|
|
||||||
resetDB()
|
// resetDB()
|
||||||
neo := &DSL{
|
// neo := &DSL{
|
||||||
ConversationSetting: conversation.Setting{
|
// Connector: "gpt-3_5-turbo",
|
||||||
Connector: "default",
|
// Option: map[string]interface{}{
|
||||||
Table: "chat_messages",
|
// "temperature": 0.7,
|
||||||
},
|
// "max_tokens": 150,
|
||||||
}
|
// },
|
||||||
|
// Prompts: []Prompt{
|
||||||
|
// {Role: "system", Content: "You are a helpful assistant"},
|
||||||
|
// },
|
||||||
|
// ConversationSetting: conversation.Setting{
|
||||||
|
// Connector: "default",
|
||||||
|
// Table: "chat_messages",
|
||||||
|
// },
|
||||||
|
// }
|
||||||
|
|
||||||
err := neo.newConversation()
|
// err := neo.newAI()
|
||||||
assert.NoError(t, err)
|
// assert.NoError(t, err)
|
||||||
ctx := Context{
|
|
||||||
Sid: "test-session",
|
|
||||||
ChatID: "test-chat",
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := &message.JSON{
|
// err = neo.newConversation()
|
||||||
Message: &message.Message{Text: "Test message"},
|
// assert.NoError(t, err)
|
||||||
}
|
|
||||||
messages := []map[string]interface{}{
|
|
||||||
{"role": "user", "content": "Hello"},
|
|
||||||
}
|
|
||||||
content := []byte("Test content")
|
|
||||||
|
|
||||||
err = neo.send(ctx, msg, messages, content, c)
|
// c.Request = httptest.NewRequest("POST", "/chat", nil)
|
||||||
assert.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_clean(t *testing.T) {
|
// neo.AI = &mockAI{}
|
||||||
defer test.Clean()
|
|
||||||
|
|
||||||
}
|
// err = neo.Answer(ctx, "Hello AI", c)
|
||||||
|
// assert.NoError(t, err)
|
||||||
|
// }
|
||||||
|
|
||||||
func resetDB() {
|
// // func TestDSL_NewAI(t *testing.T) {
|
||||||
sch := capsule.Global.Schema()
|
// // test.Prepare(t, config.Conf)
|
||||||
sch.DropTable("chat_messages")
|
// // defer Test_clean(t)
|
||||||
}
|
|
||||||
|
|
||||||
type mockAI struct{}
|
// // tests := []struct {
|
||||||
|
// // name string
|
||||||
|
// // connector string
|
||||||
|
// // wantErr string
|
||||||
|
// // }{
|
||||||
|
// // {
|
||||||
|
// // name: "Mock AI",
|
||||||
|
// // connector: "mock",
|
||||||
|
// // wantErr: "",
|
||||||
|
// // },
|
||||||
|
// // {
|
||||||
|
// // name: "Specific mock model",
|
||||||
|
// // connector: "mock:gpt-4",
|
||||||
|
// // wantErr: "",
|
||||||
|
// // },
|
||||||
|
// // {
|
||||||
|
// // name: "Invalid connector",
|
||||||
|
// // connector: "invalid-connector",
|
||||||
|
// // wantErr: "AI connector invalid-connector not found",
|
||||||
|
// // },
|
||||||
|
// // }
|
||||||
|
|
||||||
func (m *mockAI) ChatCompletionsWith(ctx context.Context, messages []map[string]interface{}, options map[string]interface{}, callback func([]byte) int) (interface{}, *exception.Exception) {
|
// // for _, tt := range tests {
|
||||||
callback([]byte(`{"choices":[{"delta":{"content":"Mock response"}}]}`))
|
// // t.Run(tt.name, func(t *testing.T) {
|
||||||
callback([]byte(`{"choices":[{"finish_reason":"stop"}]}`))
|
// // neo := &DSL{
|
||||||
return nil, nil
|
// // Connector: tt.connector,
|
||||||
}
|
// // }
|
||||||
|
// // neo.newConversation()
|
||||||
|
|
||||||
func (m *mockAI) ChatCompletions(messages []map[string]interface{}, options map[string]interface{}, callback func([]byte) int) (interface{}, *exception.Exception) {
|
// // assert.Panics(t, func() {
|
||||||
return nil, nil
|
// // neo.newAI()
|
||||||
}
|
// // })
|
||||||
|
|
||||||
func (m *mockAI) GetContent(response interface{}) (string, *exception.Exception) {
|
// // })
|
||||||
return "Mock content", nil
|
// // }
|
||||||
}
|
// // }
|
||||||
|
|
||||||
func (m *mockAI) Embeddings(input interface{}, user string) (interface{}, *exception.Exception) {
|
// func TestDSL_Select(t *testing.T) {
|
||||||
return nil, nil
|
// test.Prepare(t, config.Conf)
|
||||||
}
|
// defer Test_clean(t)
|
||||||
|
|
||||||
func (m *mockAI) Tiktoken(input string) (int, error) {
|
// resetDB()
|
||||||
return 0, nil
|
// neo := &DSL{
|
||||||
}
|
// ConversationSetting: conversation.Setting{
|
||||||
|
// Connector: "default",
|
||||||
|
// Table: "chat_messages",
|
||||||
|
// },
|
||||||
|
// }
|
||||||
|
|
||||||
func (m *mockAI) MaxToken() int {
|
// err := neo.newConversation()
|
||||||
return 4096
|
// assert.NoError(t, err)
|
||||||
}
|
|
||||||
|
// err = neo.Select("invalid-model")
|
||||||
|
// assert.Error(t, err)
|
||||||
|
|
||||||
|
// // err = neo.Select("gpt-3_5-turbo")
|
||||||
|
// // assert.NoError(t, err)
|
||||||
|
// // assert.NotNil(t, neo.AI)
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // func TestDSL_NewConversation(t *testing.T) {
|
||||||
|
// // test.Prepare(t, config.Conf)
|
||||||
|
// // defer Test_clean(t)
|
||||||
|
|
||||||
|
// // tests := []struct {
|
||||||
|
// // name string
|
||||||
|
// // connector string
|
||||||
|
// // wantErr bool
|
||||||
|
// // }{
|
||||||
|
// // {
|
||||||
|
// // name: "Default connector",
|
||||||
|
// // connector: "default",
|
||||||
|
// // wantErr: false,
|
||||||
|
// // },
|
||||||
|
// // {
|
||||||
|
// // name: "Empty connector",
|
||||||
|
// // connector: "",
|
||||||
|
// // wantErr: false,
|
||||||
|
// // },
|
||||||
|
// // {
|
||||||
|
// // name: "Invalid connector",
|
||||||
|
// // connector: "invalid-connector",
|
||||||
|
// // wantErr: true,
|
||||||
|
// // },
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// // for _, tt := range tests {
|
||||||
|
// // t.Run(tt.name, func(t *testing.T) {
|
||||||
|
// // neo := &DSL{
|
||||||
|
// // ConversationSetting: conversation.Setting{
|
||||||
|
// // Connector: tt.connector,
|
||||||
|
// // },
|
||||||
|
// // }
|
||||||
|
// // assert.Panics(t, func() {
|
||||||
|
// // neo.newConversation()
|
||||||
|
// // })
|
||||||
|
// // })
|
||||||
|
// // }
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// func TestDSL_SaveHistory(t *testing.T) {
|
||||||
|
// test.Prepare(t, config.Conf)
|
||||||
|
// defer Test_clean(t)
|
||||||
|
|
||||||
|
// neo := &DSL{
|
||||||
|
// ConversationSetting: conversation.Setting{
|
||||||
|
// Connector: "default",
|
||||||
|
// Table: "chat_messages",
|
||||||
|
// },
|
||||||
|
// }
|
||||||
|
|
||||||
|
// resetDB()
|
||||||
|
// err := neo.newConversation()
|
||||||
|
// assert.NoError(t, err)
|
||||||
|
|
||||||
|
// messages := []map[string]interface{}{
|
||||||
|
// {
|
||||||
|
// "role": "user",
|
||||||
|
// "content": "Hello",
|
||||||
|
// "name": "test-user",
|
||||||
|
// },
|
||||||
|
// }
|
||||||
|
|
||||||
|
// content := []byte("Hi there!")
|
||||||
|
// neo.saveHistory("test-session", "test-chat", content, messages)
|
||||||
|
|
||||||
|
// // Verify the history was saved
|
||||||
|
// history, err := neo.Conversation.GetHistory("test-session", "test-chat")
|
||||||
|
// assert.NoError(t, err)
|
||||||
|
// assert.NotEmpty(t, history)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func TestDSL_Send(t *testing.T) {
|
||||||
|
// test.Prepare(t, config.Conf)
|
||||||
|
// defer Test_clean(t)
|
||||||
|
|
||||||
|
// gin.SetMode(gin.TestMode)
|
||||||
|
// w := httptest.NewRecorder()
|
||||||
|
// c, _ := gin.CreateTestContext(w)
|
||||||
|
|
||||||
|
// resetDB()
|
||||||
|
// neo := &DSL{
|
||||||
|
// ConversationSetting: conversation.Setting{
|
||||||
|
// Connector: "default",
|
||||||
|
// Table: "chat_messages",
|
||||||
|
// },
|
||||||
|
// }
|
||||||
|
|
||||||
|
// err := neo.newConversation()
|
||||||
|
// assert.NoError(t, err)
|
||||||
|
// ctx := Context{
|
||||||
|
// Sid: "test-session",
|
||||||
|
// ChatID: "test-chat",
|
||||||
|
// }
|
||||||
|
|
||||||
|
// msg := &message.JSON{
|
||||||
|
// Message: &message.Message{Text: "Test message"},
|
||||||
|
// }
|
||||||
|
// messages := []map[string]interface{}{
|
||||||
|
// {"role": "user", "content": "Hello"},
|
||||||
|
// }
|
||||||
|
// content := []byte("Test content")
|
||||||
|
|
||||||
|
// err = neo.send(ctx, msg, messages, content, c)
|
||||||
|
// assert.NoError(t, err)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func Test_clean(t *testing.T) {
|
||||||
|
// defer test.Clean()
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func resetDB() {
|
||||||
|
// sch := capsule.Global.Schema()
|
||||||
|
// sch.DropTable("chat_messages")
|
||||||
|
// }
|
||||||
|
|
||||||
|
// type mockAI struct{}
|
||||||
|
|
||||||
|
// func (m *mockAI) ChatCompletionsWith(ctx context.Context, messages []map[string]interface{}, options map[string]interface{}, callback func([]byte) int) (interface{}, *exception.Exception) {
|
||||||
|
// callback([]byte(`{"choices":[{"delta":{"content":"Mock response"}}]}`))
|
||||||
|
// callback([]byte(`{"choices":[{"finish_reason":"stop"}]}`))
|
||||||
|
// return nil, nil
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func (m *mockAI) ChatCompletions(messages []map[string]interface{}, options map[string]interface{}, callback func([]byte) int) (interface{}, *exception.Exception) {
|
||||||
|
// return nil, nil
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func (m *mockAI) GetContent(response interface{}) (string, *exception.Exception) {
|
||||||
|
// return "Mock content", nil
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func (m *mockAI) Embeddings(input interface{}, user string) (interface{}, *exception.Exception) {
|
||||||
|
// return nil, nil
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func (m *mockAI) Tiktoken(input string) (int, error) {
|
||||||
|
// return 0, nil
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func (m *mockAI) MaxToken() int {
|
||||||
|
// return 4096
|
||||||
|
// }
|
||||||
|
|
|
||||||
67
neo/types.go
67
neo/types.go
|
|
@ -2,55 +2,64 @@ package neo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/yaoapp/yao/aigc"
|
"github.com/yaoapp/kun/exception"
|
||||||
|
"github.com/yaoapp/yao/neo/assistant"
|
||||||
"github.com/yaoapp/yao/neo/conversation"
|
"github.com/yaoapp/yao/neo/conversation"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DSL AI assistant
|
// DSL AI assistant
|
||||||
type DSL struct {
|
type DSL struct {
|
||||||
ID string `json:"-" yaml:"-"`
|
ID string `json:"-" yaml:"-"`
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty" yaml:"name,omitempty"`
|
||||||
Use string `json:"use,omitempty"`
|
Use string `json:"use,omitempty" yaml:"use,omitempty"` // Which assistant to use default
|
||||||
Guard string `json:"guard,omitempty"`
|
Guard string `json:"guard,omitempty" yaml:"guard,omitempty"`
|
||||||
Connector string `json:"connector"`
|
Connector string `json:"connector" yaml:"connector"`
|
||||||
ConversationSetting conversation.Setting `json:"conversation" yaml:"conversation"`
|
ConversationSetting conversation.Setting `json:"conversation" yaml:"conversation"`
|
||||||
Option map[string]interface{} `json:"option"`
|
Option map[string]interface{} `json:"option" yaml:"option"`
|
||||||
Prepare string `json:"prepare,omitempty"`
|
Prepare string `json:"prepare,omitempty" yaml:"prepare,omitempty"`
|
||||||
Write string `json:"write,omitempty"`
|
Create string `json:"create,omitempty" yaml:"create,omitempty"`
|
||||||
Prompts []aigc.Prompt `json:"prompts,omitempty"`
|
Write string `json:"write,omitempty" yaml:"write,omitempty"`
|
||||||
Allows []string `json:"allows,omitempty"`
|
AssistantListHook string `json:"assistants,omitempty" yaml:"assistants,omitempty"` // Get the assistant list from the hook
|
||||||
Models []string `json:"models,omitempty"`
|
Prompts []assistant.Prompt `json:"prompts,omitempty" yaml:"prompts,omitempty"`
|
||||||
AI aigc.AI `json:"-" yaml:"-"`
|
Allows []string `json:"allows,omitempty" yaml:"allows,omitempty"`
|
||||||
Conversation conversation.Conversation `json:"-" yaml:"-"`
|
Assistant assistant.API `json:"-" yaml:"-"` // The default assistant
|
||||||
GuardHandlers []gin.HandlerFunc `json:"-" yaml:"-"`
|
Conversation conversation.Conversation `json:"-" yaml:"-"`
|
||||||
}
|
GuardHandlers []gin.HandlerFunc `json:"-" yaml:"-"`
|
||||||
|
AssistantList []assistant.Assistant `json:"-" yaml:"-"`
|
||||||
// Answer the answer interface
|
AssistantMaps map[string]assistant.Assistant `json:"-" yaml:"-"`
|
||||||
type Answer interface {
|
|
||||||
Stream(func(w io.Writer) bool) bool
|
|
||||||
Status(code int)
|
|
||||||
Header(key, value string)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Context the context
|
// Context the context
|
||||||
type Context struct {
|
type Context struct {
|
||||||
Sid string `json:"sid" yaml:"-"`
|
Sid string `json:"sid" yaml:"-"` // Session ID
|
||||||
ChatID string `json:"chat_id,omitempty"`
|
ChatID string `json:"chat_id,omitempty"` // Chat ID, use to select chat
|
||||||
|
AssistantID string `json:"assistant_id,omitempty"` // Assistant ID, use to select assistant
|
||||||
Stack string `json:"stack,omitempty"`
|
Stack string `json:"stack,omitempty"`
|
||||||
Path string `json:"pathname,omitempty"`
|
Path string `json:"pathname,omitempty"`
|
||||||
FormData map[string]interface{} `json:"formdata,omitempty"`
|
FormData map[string]interface{} `json:"formdata,omitempty"`
|
||||||
Field *ContextField `json:"field,omitempty"`
|
Field *Field `json:"field,omitempty"`
|
||||||
Namespace string `json:"namespace,omitempty"`
|
Namespace string `json:"namespace,omitempty"`
|
||||||
Config map[string]interface{} `json:"config,omitempty"`
|
Config map[string]interface{} `json:"config,omitempty"`
|
||||||
Signal interface{} `json:"signal,omitempty"`
|
Signal interface{} `json:"signal,omitempty"`
|
||||||
context.Context `json:"-" yaml:"-"`
|
context.Context `json:"-" yaml:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ContextField the context field
|
// Field the context field
|
||||||
type ContextField struct {
|
type Field struct {
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
Bind string `json:"bind,omitempty"`
|
Bind string `json:"bind,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AI the AI interface
|
||||||
|
type AI interface {
|
||||||
|
ChatCompletions(messages []map[string]interface{}, option map[string]interface{}, cb func(data []byte) int) (interface{}, *exception.Exception)
|
||||||
|
ChatCompletionsWith(ctx context.Context, messages []map[string]interface{}, option map[string]interface{}, cb func(data []byte) int) (interface{}, *exception.Exception)
|
||||||
|
GetContent(response interface{}) (string, *exception.Exception)
|
||||||
|
Embeddings(input interface{}, user string) (interface{}, *exception.Exception)
|
||||||
|
Tiktoken(input string) (int, error)
|
||||||
|
MaxToken() int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prompt a prompt
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue