@@ -18,6 +18,7 @@ package common
1818
1919import (
2020 "fmt"
21+ "strings"
2122 "time"
2223
2324 . "github.com/onsi/ginkgo/v2"
@@ -29,16 +30,17 @@ var _ = Describe("Utils", Ordered, func() {
2930 InitRandom (time .Now ().UnixNano ())
3031 })
3132
32- Context ("GetRandomResponseText " , func () {
33+ Context ("GetRandomTokens " , func () {
3334 It ("should return complete text" , func () {
34- text , finishReason := GetRandomResponseText (nil , false )
35+ tokens , finishReason := GetRandomTokens (nil , false )
36+ text := strings .Join (tokens , "" )
3537 Expect (IsValidText (text )).To (BeTrue ())
3638 Expect (finishReason ).Should (Equal (StopFinishReason ))
3739 })
3840 It ("should return short text" , func () {
3941 maxCompletionTokens := int64 (2 )
40- text , finishReason := GetRandomResponseText (& maxCompletionTokens , false )
41- tokensCnt := int64 (len (Tokenize ( text ) ))
42+ tokens , finishReason := GetRandomTokens (& maxCompletionTokens , false )
43+ tokensCnt := int64 (len (tokens ))
4244 Expect (tokensCnt ).Should (BeNumerically ("<=" , maxCompletionTokens ))
4345 if tokensCnt == maxCompletionTokens {
4446 Expect (finishReason ).To (Equal (LengthFinishReason ))
@@ -50,9 +52,10 @@ var _ = Describe("Utils", Ordered, func() {
5052 It ("should return long text" , func () {
5153 // return required number of tokens although it is higher than ResponseLenMax
5254 maxCompletionTokens := int64 (ResponseLenMax * 5 )
53- text , finishReason := GetRandomResponseText (& maxCompletionTokens , false )
54- tokensCnt := int64 (len (Tokenize ( text ) ))
55+ tokens , finishReason := GetRandomTokens (& maxCompletionTokens , false )
56+ tokensCnt := int64 (len (tokens ))
5557 Expect (tokensCnt ).Should (BeNumerically ("<=" , maxCompletionTokens ))
58+ text := strings .Join (tokens , "" )
5659 Expect (IsValidText (text )).To (BeTrue ())
5760 if tokensCnt == maxCompletionTokens {
5861 Expect (finishReason ).To (Equal (LengthFinishReason ))
@@ -65,8 +68,8 @@ var _ = Describe("Utils", Ordered, func() {
6568 DescribeTable ("should return exact num of tokens" ,
6669 func (maxCompletionTokens int ) {
6770 n := int64 (maxCompletionTokens )
68- text , finishReason := GetRandomResponseText (& n , true )
69- nGenTokens := int64 (len (Tokenize ( text ) ))
71+ tokens , finishReason := GetRandomTokens (& n , true )
72+ nGenTokens := int64 (len (tokens ))
7073 Expect (nGenTokens ).Should (Equal (n ))
7174 Expect (finishReason ).To (Equal (LengthFinishReason ))
7275 },
@@ -80,24 +83,25 @@ var _ = Describe("Utils", Ordered, func() {
8083 )
8184 })
8285
83- Context ("GetResponseText " , func () {
86+ Context ("GetResponseTokens " , func () {
8487 theText := "Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime"
88+ theTokens := Tokenize (theText )
8589
8690 It ("should return the same text since max tokens is not defined" , func () {
87- text , finishReason := GetResponseText (nil , theText )
88- Expect (text ).Should (Equal (theText ))
91+ tokens , finishReason := GetResponseTokens (nil , theText )
92+ Expect (tokens ).Should (Equal (theTokens ))
8993 Expect (finishReason ).Should (Equal (StopFinishReason ))
9094 })
9195 It ("should return the same text since max tokens is higher than the text length" , func () {
9296 maxCompletionTokens := int64 (1000 )
93- text , finishReason := GetResponseText (& maxCompletionTokens , theText )
94- Expect (text ).Should (Equal (theText ))
97+ tokens , finishReason := GetResponseTokens (& maxCompletionTokens , theText )
98+ Expect (tokens ).Should (Equal (theTokens ))
9599 Expect (finishReason ).Should (Equal (StopFinishReason ))
96100 })
97101 It ("should return partial text" , func () {
98102 maxCompletionTokens := int64 (2 )
99- text , finishReason := GetResponseText (& maxCompletionTokens , theText )
100- Expect (int64 (len (Tokenize ( text ) ))).Should (Equal (maxCompletionTokens ))
103+ tokens , finishReason := GetResponseTokens (& maxCompletionTokens , theText )
104+ Expect (int64 (len (tokens ))).Should (Equal (maxCompletionTokens ))
101105 Expect (finishReason ).Should (Equal (LengthFinishReason ))
102106 })
103107 })
0 commit comments