Skip to content
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

feat(llms)!: Move all model config options to OpenAIOptions #232

Merged
merged 1 commit into from
Nov 21, 2023

Conversation

davidmigloz
Copy link
Owner

@davidmigloz davidmigloz commented Nov 19, 2023

Note: default completions model has been updated from text-davinci-003 to gpt-3.5-turbo-instruct. As the former will shut down on January 4th, 2024.

Until now, OpenAI completions wrapper used to accept the completions request parameters (e.g. temperature) in the constructor as parameters. Like:

final llm = OpenAI(apiKey: openaiApiKey, temperature: 0.9);

This was not ideal, as it was not possible to change the parameters after the object was created. So, for example, if you wanted to change the temperature, you had to create a new instance.

Now, all the completions request parameters are part of the OpenAIOptions class.

When you create an instance of OpenAI, you can define the defaultOptions that will be used for all the completions requests.

If you want to change the parameters for a specific request, you can pass the desired OpenAIOptions when making the request. The options passed in the request will override the defaultOptions.

Migration

Move all the request parameters from the constructor to the defaultOptions parameter.

Before:

final llm = OpenAI(
  apiKey: openaiApiKey,
  temperature: 0.9,
  maxTokens: 100,
);

Now:

final llm = OpenAI(
  apiKey: openaiApiKey,
  defaultOptions: const OpenAIOptions(
    temperature: 0.9,
    maxTokens: 100,
  ),
);

Example of using different options for different requests

final prompt = PromptValue.string('How are you?');
final llm = OpenAI(
  apiKey: openaiApiKey,
  defaultOptions: const OpenAIOptions(
    temperature: 0,
    maxTokens: 50,
  ),
);

final res1 = await llm.invoke(prompt);

final res2 = await llm.invoke(
  prompt,
  options: const OpenAIOptions(seed: 9999),
);

You can also change the options in a Runnable pipeline using the bind method. In this case, we are using two totally different models for each question:

final llm = OpenAI(apiKey: openaiApiKey,);
const outputParser = StringOutputParser();
final prompt1 = PromptTemplate.fromTemplate('How are you {name}?');
final prompt2 = PromptTemplate.fromTemplate('How old are you {name}?');
final chain = Runnable.fromMap({
  'q1': prompt1 | llm.bind(const OpenAIOptions(model: 'gpt-3.5-turbo-instruct')) | outputParser,
  'q2': prompt2| llm.bind(const OpenAIOptions(model: 'text-davinci-003')) | outputParser,
});
final res = await chain.invoke({'name': 'David'});

@davidmigloz davidmigloz self-assigned this Nov 19, 2023
@davidmigloz davidmigloz added this to the v0.2.0 milestone Nov 19, 2023
@davidmigloz davidmigloz added t:enhancement New feature or request c:llms Models and integrations. p:langchain_openai langchain_openai package. labels Nov 19, 2023
@davidmigloz davidmigloz force-pushed the openai-default-options branch 3 times, most recently from 1f1ca9c to 765a5b4 Compare November 21, 2023 18:57
@davidmigloz davidmigloz force-pushed the openai-default-options branch 2 times, most recently from f742d94 to 4ea127b Compare November 21, 2023 20:51
@davidmigloz davidmigloz force-pushed the openai-default-options branch from 4ea127b to c33955c Compare November 21, 2023 21:21
@davidmigloz davidmigloz merged commit 16e3e8e into main Nov 21, 2023
1 check passed
@davidmigloz davidmigloz deleted the openai-default-options branch November 21, 2023 21:33
@davidmigloz davidmigloz added the c:lcel LangChain Expression Language label Nov 25, 2023
KennethKnudsen97 pushed a commit to KennethKnudsen97/langchain_dart that referenced this pull request Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c:lcel LangChain Expression Language c:llms Models and integrations. p:langchain_openai langchain_openai package. t:enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant