Skip to content

Commit 3930e2b

Browse files
authored
chore: add openai response type (#199)
1 parent cbc913b commit 3930e2b

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/henomis/lingoose/llm/openai"
8+
"github.com/henomis/lingoose/thread"
9+
)
10+
11+
func main() {
12+
openaillm := openai.New().WithModel(openai.GPT4o).WithResponseFormat(openai.ResponseFormatJSONObject).WithMaxTokens(1000)
13+
14+
t := thread.New().AddMessage(
15+
thread.NewUserMessage().AddContent(
16+
thread.NewTextContent("Give me a JSON object that describes a person"),
17+
),
18+
)
19+
20+
err := openaillm.Generate(context.Background(), t)
21+
if err != nil {
22+
panic(err)
23+
}
24+
25+
fmt.Println(t)
26+
}

llm/openai/common.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,10 @@ const (
5757

5858
type UsageCallback func(types.Meta)
5959
type StreamCallback func(string)
60+
61+
type ResponseFormat = openai.ChatCompletionResponseFormatType
62+
63+
const (
64+
ResponseFormatJSONObject ResponseFormat = openai.ChatCompletionResponseFormatTypeJSONObject
65+
ResponseFormatText ResponseFormat = openai.ChatCompletionResponseFormatTypeText
66+
)

llm/openai/openai.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type OpenAI struct {
3838
usageCallback UsageCallback
3939
functions map[string]Function
4040
streamCallbackFn StreamCallback
41+
responseFormat *ResponseFormat
4142
toolChoice *string
4243
cache *cache.Cache
4344
Name string
@@ -99,6 +100,11 @@ func (o *OpenAI) WithCache(cache *cache.Cache) *OpenAI {
99100
return o
100101
}
101102

103+
func (o *OpenAI) WithResponseFormat(responseFormat ResponseFormat) *OpenAI {
104+
o.responseFormat = &responseFormat
105+
return o
106+
}
107+
102108
// SetStop sets the stop sequences for the completion.
103109
func (o *OpenAI) SetStop(stop []string) {
104110
o.stop = stop
@@ -347,14 +353,22 @@ func (o *OpenAI) generate(
347353
}
348354

349355
func (o *OpenAI) buildChatCompletionRequest(t *thread.Thread) openai.ChatCompletionRequest {
356+
var responseFormat *openai.ChatCompletionResponseFormat
357+
if o.responseFormat != nil {
358+
responseFormat = &openai.ChatCompletionResponseFormat{
359+
Type: *o.responseFormat,
360+
}
361+
}
362+
350363
return openai.ChatCompletionRequest{
351-
Model: string(o.model),
352-
Messages: threadToChatCompletionMessages(t),
353-
MaxTokens: o.maxTokens,
354-
Temperature: o.temperature,
355-
N: DefaultOpenAINumResults,
356-
TopP: DefaultOpenAITopP,
357-
Stop: o.stop,
364+
Model: string(o.model),
365+
Messages: threadToChatCompletionMessages(t),
366+
MaxTokens: o.maxTokens,
367+
Temperature: o.temperature,
368+
N: DefaultOpenAINumResults,
369+
TopP: DefaultOpenAITopP,
370+
Stop: o.stop,
371+
ResponseFormat: responseFormat,
358372
}
359373
}
360374

0 commit comments

Comments
 (0)