Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
0aff6787
Unverified
Commit
0aff6787
authored
Jul 09, 2024
by
royjhan
Committed by
GitHub
Jul 09, 2024
Browse files
separate request tests (#5578)
parent
9544a57e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
75 additions
and
113 deletions
+75
-113
openai/openai_test.go
openai/openai_test.go
+75
-113
No files found.
openai/openai_test.go
View file @
0aff6787
...
...
@@ -3,7 +3,6 @@ package openai
import
(
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
...
...
@@ -16,49 +15,33 @@ import (
"github.com/stretchr/testify/assert"
)
func
TestMiddleware
(
t
*
testing
.
T
)
{
func
TestMiddleware
Requests
(
t
*
testing
.
T
)
{
type
testCase
struct
{
Name
string
Method
string
Path
string
TestPath
string
Handler
func
()
gin
.
HandlerFunc
Endpoint
func
(
c
*
gin
.
Context
)
Setup
func
(
t
*
testing
.
T
,
req
*
http
.
Request
)
Expected
func
(
t
*
testing
.
T
,
re
sp
*
http
test
.
ResponseRecorder
)
Expected
func
(
t
*
testing
.
T
,
re
q
*
http
.
Request
)
}
testCases
:=
[]
testCase
{
{
Name
:
"chat handler"
,
Method
:
http
.
MethodPost
,
Path
:
"/api/chat"
,
TestPath
:
"/api/chat"
,
Handler
:
ChatMiddleware
,
Endpoint
:
func
(
c
*
gin
.
Context
)
{
var
chatReq
api
.
ChatRequest
if
err
:=
c
.
ShouldBindJSON
(
&
chatReq
);
err
!=
nil
{
c
.
JSON
(
http
.
StatusBadRequest
,
gin
.
H
{
"error"
:
"invalid request"
})
return
}
userMessage
:=
chatReq
.
Messages
[
0
]
.
Content
var
assistantMessage
string
var
capturedRequest
*
http
.
Request
switch
userMessage
{
case
"Hello"
:
assistantMessage
=
"Hello!"
default
:
assistantMessage
=
"I'm not sure how to respond to that."
}
captureRequestMiddleware
:=
func
()
gin
.
HandlerFunc
{
return
func
(
c
*
gin
.
Context
)
{
bodyBytes
,
_
:=
io
.
ReadAll
(
c
.
Request
.
Body
)
c
.
Request
.
Body
=
io
.
NopCloser
(
bytes
.
NewReader
(
bodyBytes
))
capturedRequest
=
c
.
Request
c
.
Next
()
}
}
c
.
JSON
(
http
.
StatusOK
,
api
.
ChatResponse
{
Message
:
api
.
Message
{
Role
:
"assistant"
,
Content
:
assistantMessage
,
},
})
},
testCases
:=
[]
testCase
{
{
Name
:
"chat handler"
,
Method
:
http
.
MethodPost
,
Path
:
"/api/chat"
,
Handler
:
ChatMiddleware
,
Setup
:
func
(
t
*
testing
.
T
,
req
*
http
.
Request
)
{
body
:=
ChatCompletionRequest
{
Model
:
"test-model"
,
...
...
@@ -70,38 +53,32 @@ func TestMiddleware(t *testing.T) {
req
.
Body
=
io
.
NopCloser
(
bytes
.
NewReader
(
bodyBytes
))
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
},
Expected
:
func
(
t
*
testing
.
T
,
resp
*
httptest
.
ResponseRecorder
)
{
assert
.
Equal
(
t
,
http
.
StatusOK
,
resp
.
Code
)
var
chatResp
ChatCompletion
if
err
:=
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
chatResp
);
err
!=
nil
{
Expected
:
func
(
t
*
testing
.
T
,
req
*
http
.
Request
)
{
var
chatReq
api
.
ChatRequest
if
err
:=
json
.
NewDecoder
(
req
.
Body
)
.
Decode
(
&
chatReq
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
chatRe
sp
.
Object
!=
"chat.completion
"
{
t
.
Fatalf
(
"expected
chat.completion
, got %s"
,
chatRe
sp
.
Object
)
if
chatRe
q
.
Messages
[
0
]
.
Role
!=
"user
"
{
t
.
Fatalf
(
"expected
'user'
, got %s"
,
chatRe
q
.
Messages
[
0
]
.
Role
)
}
if
chatRe
sp
.
Choices
[
0
]
.
Message
.
Content
!=
"Hello
!
"
{
t
.
Fatalf
(
"expected Hello
!
, got %s"
,
chatRe
sp
.
Choices
[
0
]
.
Message
.
Content
)
if
chatRe
q
.
Message
s
[
0
]
.
Content
!=
"Hello"
{
t
.
Fatalf
(
"expected
'
Hello
'
, got %s"
,
chatRe
q
.
Message
s
[
0
]
.
Content
)
}
},
},
{
Name
:
"completions handler"
,
Method
:
http
.
MethodPost
,
Path
:
"/api/generate"
,
TestPath
:
"/api/generate"
,
Handler
:
CompletionsMiddleware
,
Endpoint
:
func
(
c
*
gin
.
Context
)
{
c
.
JSON
(
http
.
StatusOK
,
api
.
GenerateResponse
{
Response
:
"Hello!"
,
})
},
Name
:
"completions handler"
,
Method
:
http
.
MethodPost
,
Path
:
"/api/generate"
,
Handler
:
CompletionsMiddleware
,
Setup
:
func
(
t
*
testing
.
T
,
req
*
http
.
Request
)
{
temp
:=
float32
(
0.8
)
body
:=
CompletionRequest
{
Model
:
"test-model"
,
Prompt
:
"Hello"
,
Model
:
"test-model"
,
Prompt
:
"Hello"
,
Temperature
:
&
temp
,
}
bodyBytes
,
_
:=
json
.
Marshal
(
body
)
...
...
@@ -109,80 +86,65 @@ func TestMiddleware(t *testing.T) {
req
.
Body
=
io
.
NopCloser
(
bytes
.
NewReader
(
bodyBytes
))
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
},
Expected
:
func
(
t
*
testing
.
T
,
resp
*
httptest
.
ResponseRecorder
)
{
assert
.
Equal
(
t
,
http
.
StatusOK
,
resp
.
Code
)
var
completionResp
Completion
if
err
:=
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
completionResp
);
err
!=
nil
{
Expected
:
func
(
t
*
testing
.
T
,
req
*
http
.
Request
)
{
var
genReq
api
.
GenerateRequest
if
err
:=
json
.
NewDecoder
(
req
.
Body
)
.
Decode
(
&
genReq
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
completionResp
.
Object
!=
"text_completion
"
{
t
.
Fatalf
(
"expected
text_completion, got %s"
,
completionResp
.
Objec
t
)
if
genReq
.
Prompt
!=
"Hello
"
{
t
.
Fatalf
(
"expected
'Hello', got %s"
,
genReq
.
Promp
t
)
}
if
completionResp
.
Choices
[
0
]
.
Text
!=
"Hello!"
{
t
.
Fatalf
(
"expected
Hello!
, got %
s
"
,
completionResp
.
Choices
[
0
]
.
Text
)
if
genReq
.
Options
[
"temperature"
]
!=
1.6
{
t
.
Fatalf
(
"expected
1.6
, got %
f
"
,
genReq
.
Options
[
"temperature"
]
)
}
},
},
{
Name
:
"completions handler with params"
,
Method
:
http
.
MethodPost
,
Path
:
"/api/generate"
,
TestPath
:
"/api/generate"
,
Handler
:
CompletionsMiddleware
,
Endpoint
:
func
(
c
*
gin
.
Context
)
{
var
generateReq
api
.
GenerateRequest
if
err
:=
c
.
ShouldBindJSON
(
&
generateReq
);
err
!=
nil
{
c
.
JSON
(
http
.
StatusBadRequest
,
gin
.
H
{
"error"
:
"invalid request"
})
return
}
}
temperature
:=
generateReq
.
Options
[
"temperature"
]
.
(
float64
)
var
assistantMessage
string
gin
.
SetMode
(
gin
.
TestMode
)
router
:=
gin
.
New
()
switch
temperature
{
case
1.6
:
assistantMessage
=
"Received temperature of 1.6"
default
:
assistantMessage
=
fmt
.
Sprintf
(
"Received temperature of %f"
,
temperature
)
}
endpoint
:=
func
(
c
*
gin
.
Context
)
{
c
.
Status
(
http
.
StatusOK
)
}
c
.
JSON
(
http
.
StatusOK
,
api
.
GenerateResponse
{
Response
:
assistantMessage
,
})
},
Setup
:
func
(
t
*
testing
.
T
,
req
*
http
.
Request
)
{
temp
:=
float32
(
0.8
)
body
:=
CompletionRequest
{
Model
:
"test-model"
,
Prompt
:
"Hello"
,
Temperature
:
&
temp
,
}
for
_
,
tc
:=
range
testCases
{
t
.
Run
(
tc
.
Name
,
func
(
t
*
testing
.
T
)
{
router
=
gin
.
New
()
router
.
Use
(
captureRequestMiddleware
())
router
.
Use
(
tc
.
Handler
())
router
.
Handle
(
tc
.
Method
,
tc
.
Path
,
endpoint
)
req
,
_
:=
http
.
NewRequest
(
tc
.
Method
,
tc
.
Path
,
nil
)
bodyBytes
,
_
:=
json
.
Marshal
(
body
)
if
tc
.
Setup
!=
nil
{
tc
.
Setup
(
t
,
req
)
}
req
.
Body
=
io
.
NopCloser
(
bytes
.
NewReader
(
bodyBytes
))
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
},
Expected
:
func
(
t
*
testing
.
T
,
resp
*
httptest
.
ResponseRecorder
)
{
assert
.
Equal
(
t
,
http
.
StatusOK
,
resp
.
Code
)
var
completionResp
Completion
if
err
:=
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
completionResp
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
resp
:=
httptest
.
NewRecorder
()
router
.
ServeHTTP
(
resp
,
req
)
if
completionResp
.
Object
!=
"text_completion"
{
t
.
Fatalf
(
"expected text_completion, got %s"
,
completionResp
.
Object
)
}
tc
.
Expected
(
t
,
capturedRequest
)
})
}
}
if
completionResp
.
Choices
[
0
]
.
Text
!=
"Received temperature of 1.6"
{
t
.
Fatalf
(
"expected Received temperature of 1.6, got %s"
,
completionResp
.
Choices
[
0
]
.
Text
)
}
},
},
func
TestMiddlewareResponses
(
t
*
testing
.
T
)
{
type
testCase
struct
{
Name
string
Method
string
Path
string
TestPath
string
Handler
func
()
gin
.
HandlerFunc
Endpoint
func
(
c
*
gin
.
Context
)
Setup
func
(
t
*
testing
.
T
,
req
*
http
.
Request
)
Expected
func
(
t
*
testing
.
T
,
resp
*
httptest
.
ResponseRecorder
)
}
testCases
:=
[]
testCase
{
{
Name
:
"completions handler
with
error"
,
Name
:
"completions handler error
forwarding
"
,
Method
:
http
.
MethodPost
,
Path
:
"/api/generate"
,
TestPath
:
"/api/generate"
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment