-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add response_format support for openai and a way to set the functions #170
Conversation
hi, I do need this feature too. Is there a plan to merge this PR? thanks @henomis |
@henomis please let me know, what you think should be changed. Would be happy to incorporate it. |
// WithFunctions sets the functions to use for the OpenAI instance. | ||
func (o *OpenAI) WithFunctions(functions map[string]Function) *OpenAI { | ||
o.functions = functions | ||
return o | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is out of context of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this out of scope?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure of this method and this change must be discussed.
@@ -37,6 +37,7 @@ type OpenAI struct { | |||
streamCallbackFn StreamCallback | |||
toolChoice *string | |||
cache *cache.Cache | |||
responseFormat openai.ChatCompletionResponseFormatType |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not use go-openai type here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use a local alias for this or what is your preference?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes use an alias, see the comment below.
func (o *OpenAI) WithToolChoice(toolChoice *string) *OpenAI { | ||
o.toolChoice = toolChoice | ||
return o | ||
} | ||
|
||
func (o *OpenAI) WithResponseFormat(format openai.ChatCompletionResponseFormatType) *OpenAI { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use go-openai types here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lingoose's users must deal with lingoose types.
temperature: DefaultOpenAITemperature, | ||
maxTokens: DefaultOpenAIMaxTokens, | ||
functions: make(map[string]Function), | ||
responseFormat: openai.ChatCompletionResponseFormatTypeText, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use go-openai types here.
@@ -299,6 +312,9 @@ func (o *OpenAI) buildChatCompletionRequest(t *thread.Thread) openai.ChatComplet | |||
N: DefaultOpenAINumResults, | |||
TopP: DefaultOpenAITopP, | |||
Stop: o.stop, | |||
ResponseFormat: &openai.ChatCompletionResponseFormat{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ResponseFormat
is omitempty
in go-openai package. This means that you can use a default pointer to nil
and eventually overwrite it using the internal value.
@@ -208,7 +208,7 @@ func (c *Content) Format(input types.M) *Content { | |||
prompt := prompt.NewPromptTemplate(c.Data.(string)) | |||
err := prompt.Format(input) | |||
if err != nil { | |||
return c | |||
panic(c) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't panic. Out of scope of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an interesting question as we should at least surface the error. This tripped me up a few times as the llm was just being prompted with an empty prompt essentially. Should we log or what is your preference?
github.com/sashabaranov/go-openai v1.21.0 h1:isAf3zPSD3VLd0pC2/2Q6ZyRK7jzPAaz+X3rjsviaYQ= | ||
github.com/sashabaranov/go-openai v1.21.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is out of scope of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, I will remove this.
GPT4Turbo Model = openai.GPT4Turbo | ||
GPT4Turbo20240409 Model = openai.GPT4Turbo20240409 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of Scope
@@ -324,7 +324,7 @@ func (o *OpenAI) getChatCompletionRequestTools() []openai.Tool { | |||
for _, function := range o.functions { | |||
tools = append(tools, openai.Tool{ | |||
Type: "function", | |||
Function: openai.FunctionDefinition{ | |||
Function: &openai.FunctionDefinition{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of scope
@@ -37,6 +37,7 @@ type OpenAI struct { | |||
streamCallbackFn StreamCallback | |||
toolChoice *string | |||
cache *cache.Cache | |||
responseFormat openai.ChatCompletionResponseFormatType |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes use an alias, see the comment below.
func (o *OpenAI) WithToolChoice(toolChoice *string) *OpenAI { | ||
o.toolChoice = toolChoice | ||
return o | ||
} | ||
|
||
func (o *OpenAI) WithResponseFormat(format openai.ChatCompletionResponseFormatType) *OpenAI { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lingoose's users must deal with lingoose types.
// WithFunctions sets the functions to use for the OpenAI instance. | ||
func (o *OpenAI) WithFunctions(functions map[string]Function) *OpenAI { | ||
o.functions = functions | ||
return o | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure of this method and this change must be discussed.
Hi, @henomis It seems that this PR has been on hold for a long time. Does the lingoose project have any plans to develop this feature? To be honest, this feature is still very important for openai. I also know that this PR is currently in a very awkward situation, and the author may not be able to continue development due to some unknown reasons. It would be a very commendable thing if the project can integrate this feature. |
@BabySid |
Implemented here: #199 |
Many thanks:) |
Description
I needed support for the JSON output that OpenAI supports by setting the type. This is already supported by the underlying client.
Type of change
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
Checklist: