-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9229de
commit e010cc1
Showing
3 changed files
with
168 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,92 @@ | ||
package telegram | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"talk2robots/m/v2/app/db/mongo" | ||
"talk2robots/m/v2/app/db/redis" | ||
"talk2robots/m/v2/app/lib" | ||
"talk2robots/m/v2/app/models" | ||
"talk2robots/m/v2/app/openai" | ||
"testing" | ||
"time" | ||
|
||
"github.com/mymmrac/telego" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/undefinedlabs/go-mpatch" | ||
) | ||
|
||
func TestProcessChatCompleteStreamingMessage(t *testing.T) { | ||
func init() { | ||
setupTestDatadog() | ||
|
||
redis.RedisClient = redis.NewMockRedisClient() | ||
|
||
mongo.MongoDBClient = mongo.NewMockMongoDBClient( | ||
models.MongoUser{ | ||
ID: "123", | ||
Usage: 0.1, | ||
}, | ||
) | ||
|
||
setupTestBot() | ||
setupCommandHandlers() | ||
|
||
log.SetFormatter(&log.TextFormatter{ | ||
FullTimestamp: true, | ||
ForceColors: true, | ||
}) | ||
log.SetLevel(log.DebugLevel) | ||
} | ||
|
||
func TestProcessThreadedStreamingMessage(t *testing.T) { | ||
message := telego.Message{ | ||
Chat: telego.Chat{ | ||
ID: 123, | ||
Type: "private", | ||
}, | ||
Text: "Tell me about 25 most famous Jedi and write two paragraphs about each of them", | ||
} | ||
ctx, cancelContext := context.WithCancel(context.Background()) | ||
ctx = context.WithValue(ctx, models.UserContext{}, "123") | ||
ctx = context.WithValue(ctx, models.ClientContext{}, "telegram") | ||
ctx = context.WithValue(ctx, models.ChannelContext{}, "123") | ||
|
||
// OpenAI API patch | ||
openAIPatch, _ := mpatch.PatchInstanceMethodByName( | ||
reflect.TypeOf(BOT.API), | ||
"CreateThreadAndRunStreaming", | ||
func(a *openai.API, ctx context.Context, assistantId string, model models.Engine, thread *models.Thread, cancelContext context.CancelFunc) (chan string, error) { | ||
messages := make(chan string) | ||
go func() { | ||
defer close(messages) | ||
defer cancelContext() | ||
message := "This is one message of 256 characters. This is one message of ___ characters. This is one message of ___ characters. This is one message of ___ characters. This is one message of ___ characters. This is one message of ___ characters. This is one longgggg.\n" | ||
for i := 0; i < 60; i++ { | ||
// sleep for 1 second | ||
<-time.After(60 * time.Millisecond) | ||
messages <- message | ||
} | ||
}() | ||
return messages, nil | ||
}, | ||
) | ||
defer openAIPatch.Unpatch() | ||
|
||
// SendMessage patch | ||
sendMessagePatch, _ := mpatch.PatchInstanceMethodByName( | ||
reflect.TypeOf(BOT.Bot), | ||
"SendMessage", | ||
getSendMessageFuncAssertion(t, "...", 123), | ||
) | ||
defer sendMessagePatch.Unpatch() | ||
|
||
// EditMessage patch | ||
editMessagePatch, _ := mpatch.PatchInstanceMethodByName( | ||
reflect.TypeOf(BOT.Bot), | ||
"EditMessageText", | ||
getEditMessageFuncAssertion(t, "This is one message of 256 characters", 123), | ||
) | ||
defer editMessagePatch.Unpatch() | ||
|
||
ProcessThreadedStreamingMessage(ctx, BOT.Bot, &message, lib.ChatGPT, models.ChatGpt35Turbo, cancelContext) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters