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

support LMStudio when using ChatOpenAI #243

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ Currently supported AI services:
- Ollama
- Mistral
- Bumblebee self-hosted models - including Llama, Mistral and Zephyr
- [LMStudio](https://lmstudio.ai/docs/api/endpoints/openai) via their OpenAI compatibility API

**LangChain** is short for Language Chain. An LLM, or Large Language Model, is the "Language" part. This library makes it easier for Elixir applications to "chain" or connect different processes, integrations, libraries, services, or functionality together with an LLM.

@@ -124,11 +125,11 @@ A list of models to use:

## Prompt caching

ChatGPT and Claude both offer prefix-based prompt caching, which can offer cost and performance benefits for longer prompts. Gemini offers context caching, which is similar.
ChatGPT and Claude both offer prefix-based prompt caching, which can offer cost and performance benefits for longer prompts. Gemini offers context caching, which is similar.

- [ChatGPT's prompt caching](https://openai.com/index/api-prompt-caching/) is automatic for prompts longer than 1024 tokens, caching the longest common prefix.
- [Claude's prompt caching](https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching) is not automatic. It's prefixing processes tools, system, and then messages, in that order, up to and including the block designated with {"cache_control": {"type": "ephemeral"}} . See LangChain.ChatModels.ChatAnthropicTest and for an example.
- [Gemini's context caching]((https://ai.google.dev/gemini-api/docs/caching?lang=python)) requires a seperate call which is not supported by Langchain.
- [Gemini's context caching]((https://ai.google.dev/gemini-api/docs/caching?lang=python)) requires a seperate call which is not supported by Langchain.

## Usage

8 changes: 6 additions & 2 deletions lib/chat_models/chat_open_ai.ex
Original file line number Diff line number Diff line change
@@ -312,9 +312,9 @@ defmodule LangChain.ChatModels.ChatOpenAI do
end
end)
|> Enum.reverse(),
response_format: set_response_format(openai),
user: openai.user
}
|> Utils.conditionally_add_to_map(:response_format, set_response_format(openai))
|> Utils.conditionally_add_to_map(
:reasoning_effort,
if(openai.reasoning_mode, do: openai.reasoning_effort, else: nil)
@@ -357,7 +357,11 @@ defmodule LangChain.ChatModels.ChatOpenAI do
end

defp set_response_format(%ChatOpenAI{json_response: false}) do
%{"type" => "text"}
# NOTE: The default handling when unspecified is `%{"type" => "text"}`
#
# For improved compatibility with other APIs like LMStudio, this returns a
# `nil` which has the same effect.
nil
end

defp get_tool_choice(%ChatOpenAI{

Unchanged files with check annotations Beta

end
describe "for_api/3" do
test "generates a map for an API call" do

Check failure on line 110 in test/chat_models/chat_open_ai_test.exs

GitHub Actions / Build and test

test for_api/3 generates a map for an API call (LangChain.ChatModels.ChatOpenAITest)
{:ok, openai} =
ChatOpenAI.new(%{
"model" => @test_model,
end
describe "set_response_format/1" do
test "generates a map for an API call with text format when json_response is false" do

Check failure on line 2172 in test/chat_models/chat_open_ai_test.exs

GitHub Actions / Build and test

test set_response_format/1 generates a map for an API call with text format when json_response is false (LangChain.ChatModels.ChatOpenAITest)
{:ok, openai} =
ChatOpenAI.new(%{
model: @test_model,