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
5f805118
Unverified
Commit
5f805118
authored
Nov 29, 2024
by
Parth Sareen
Committed by
GitHub
Nov 29, 2024
Browse files
Enable index tracking for tools - openai api support (#7888)
parent
39e29ae5
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
89 additions
and
4 deletions
+89
-4
api/types.go
api/types.go
+1
-0
openai/openai.go
openai/openai.go
+2
-0
openai/openai_test.go
openai/openai_test.go
+80
-1
server/routes.go
server/routes.go
+6
-3
No files found.
api/types.go
View file @
5f805118
...
@@ -146,6 +146,7 @@ type ToolCall struct {
...
@@ -146,6 +146,7 @@ type ToolCall struct {
}
}
type
ToolCallFunction
struct
{
type
ToolCallFunction
struct
{
Index
int
`json:"index,omitempty"`
Name
string
`json:"name"`
Name
string
`json:"name"`
Arguments
ToolCallFunctionArguments
`json:"arguments"`
Arguments
ToolCallFunctionArguments
`json:"arguments"`
}
}
...
...
openai/openai.go
View file @
5f805118
...
@@ -140,6 +140,7 @@ type CompletionChunk struct {
...
@@ -140,6 +140,7 @@ type CompletionChunk struct {
type
ToolCall
struct
{
type
ToolCall
struct
{
ID
string
`json:"id"`
ID
string
`json:"id"`
Index
int
`json:"index"`
Type
string
`json:"type"`
Type
string
`json:"type"`
Function
struct
{
Function
struct
{
Name
string
`json:"name"`
Name
string
`json:"name"`
...
@@ -206,6 +207,7 @@ func toToolCalls(tc []api.ToolCall) []ToolCall {
...
@@ -206,6 +207,7 @@ func toToolCalls(tc []api.ToolCall) []ToolCall {
toolCalls
[
i
]
.
ID
=
toolCallId
()
toolCalls
[
i
]
.
ID
=
toolCallId
()
toolCalls
[
i
]
.
Type
=
"function"
toolCalls
[
i
]
.
Type
=
"function"
toolCalls
[
i
]
.
Function
.
Name
=
tc
.
Function
.
Name
toolCalls
[
i
]
.
Function
.
Name
=
tc
.
Function
.
Name
toolCalls
[
i
]
.
Index
=
tc
.
Function
.
Index
args
,
err
:=
json
.
Marshal
(
tc
.
Function
.
Arguments
)
args
,
err
:=
json
.
Marshal
(
tc
.
Function
.
Arguments
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
openai/openai_test.go
View file @
5f805118
...
@@ -195,7 +195,86 @@ func TestChatMiddleware(t *testing.T) {
...
@@ -195,7 +195,86 @@ func TestChatMiddleware(t *testing.T) {
Stream
:
&
False
,
Stream
:
&
False
,
},
},
},
},
{
name
:
"chat handler with streaming tools"
,
body
:
`{
"model": "test-model",
"messages": [
{"role": "user", "content": "What's the weather like in Paris?"}
],
"stream": true,
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather",
"parameters": {
"type": "object",
"required": ["location"],
"properties": {
"location": {
"type": "string",
"description": "The city and state"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
}
}
}
}]
}`
,
req
:
api
.
ChatRequest
{
Model
:
"test-model"
,
Messages
:
[]
api
.
Message
{
{
Role
:
"user"
,
Content
:
"What's the weather like in Paris?"
,
},
},
Tools
:
[]
api
.
Tool
{
{
Type
:
"function"
,
Function
:
api
.
ToolFunction
{
Name
:
"get_weather"
,
Description
:
"Get the current weather"
,
Parameters
:
struct
{
Type
string
`json:"type"`
Required
[]
string
`json:"required"`
Properties
map
[
string
]
struct
{
Type
string
`json:"type"`
Description
string
`json:"description"`
Enum
[]
string
`json:"enum,omitempty"`
}
`json:"properties"`
}{
Type
:
"object"
,
Required
:
[]
string
{
"location"
},
Properties
:
map
[
string
]
struct
{
Type
string
`json:"type"`
Description
string
`json:"description"`
Enum
[]
string
`json:"enum,omitempty"`
}{
"location"
:
{
Type
:
"string"
,
Description
:
"The city and state"
,
},
"unit"
:
{
Type
:
"string"
,
Enum
:
[]
string
{
"celsius"
,
"fahrenheit"
},
},
},
},
},
},
},
Options
:
map
[
string
]
any
{
"temperature"
:
1.0
,
"top_p"
:
1.0
,
},
Stream
:
&
True
,
},
},
{
{
name
:
"chat handler error forwarding"
,
name
:
"chat handler error forwarding"
,
body
:
`{
body
:
`{
...
...
server/routes.go
View file @
5f805118
...
@@ -1469,7 +1469,7 @@ func (s *Server) ChatHandler(c *gin.Context) {
...
@@ -1469,7 +1469,7 @@ func (s *Server) ChatHandler(c *gin.Context) {
go
func
()
{
go
func
()
{
defer
close
(
ch
)
defer
close
(
ch
)
var
sb
strings
.
Builder
var
sb
strings
.
Builder
var
hasT
oolCall
s
bool
var
t
oolCall
Index
int
=
0
if
err
:=
r
.
Completion
(
c
.
Request
.
Context
(),
llm
.
CompletionRequest
{
if
err
:=
r
.
Completion
(
c
.
Request
.
Context
(),
llm
.
CompletionRequest
{
Prompt
:
prompt
,
Prompt
:
prompt
,
Images
:
images
,
Images
:
images
,
...
@@ -1509,16 +1509,19 @@ func (s *Server) ChatHandler(c *gin.Context) {
...
@@ -1509,16 +1509,19 @@ func (s *Server) ChatHandler(c *gin.Context) {
sb
.
WriteString
(
r
.
Content
)
sb
.
WriteString
(
r
.
Content
)
if
toolCalls
,
ok
:=
m
.
parseToolCalls
(
sb
.
String
());
ok
{
if
toolCalls
,
ok
:=
m
.
parseToolCalls
(
sb
.
String
());
ok
{
res
.
Message
.
ToolCalls
=
toolCalls
res
.
Message
.
ToolCalls
=
toolCalls
for
i
:=
range
toolCalls
{
toolCalls
[
i
]
.
Function
.
Index
=
toolCallIndex
toolCallIndex
++
}
res
.
Message
.
Content
=
""
res
.
Message
.
Content
=
""
sb
.
Reset
()
sb
.
Reset
()
hasToolCalls
=
true
ch
<-
res
ch
<-
res
return
return
}
}
if
r
.
Done
{
if
r
.
Done
{
// Send any remaining content if no tool calls were detected
// Send any remaining content if no tool calls were detected
if
!
hasT
oolCall
s
{
if
t
oolCall
Index
==
0
{
res
.
Message
.
Content
=
sb
.
String
()
res
.
Message
.
Content
=
sb
.
String
()
}
}
ch
<-
res
ch
<-
res
...
...
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