Skip to content

Commit

Permalink
Merge pull request #78 from brainlid/me-prep-for-release
Browse files Browse the repository at this point in the history
prep for release v0.1.8
  • Loading branch information
brainlid authored Feb 16, 2024
2 parents 0438351 + 8d97f3a commit 76ed163
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 23 deletions.
9 changes: 7 additions & 2 deletions lib/chains/llm_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,16 @@ defmodule LangChain.Chains.LLMChain do
{:ok, [[%MessageDelta{} | _] | _] = deltas} ->
if chain.verbose_deltas, do: IO.inspect(deltas, label: "DELTA MESSAGE LIST RESPONSE")
updated_chain = apply_deltas(chain, deltas)
if chain.verbose, do: IO.inspect(updated_chain.last_message, label: "COMBINED DELTA MESSAGE RESPONSE")

if chain.verbose,
do: IO.inspect(updated_chain.last_message, label: "COMBINED DELTA MESSAGE RESPONSE")

{:ok, updated_chain}

{:ok, %Message{} = message} ->
if chain.verbose, do: IO.inspect(message, label: "SINGLE MESSAGE RESPONSE NO WRAPPED ARRAY")
if chain.verbose,
do: IO.inspect(message, label: "SINGLE MESSAGE RESPONSE NO WRAPPED ARRAY")

{:ok, add_message(chain, message)}

{:error, reason} ->
Expand Down
1 change: 0 additions & 1 deletion lib/chains/text_to_title_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ defmodule LangChain.Chains.TextToTitleChain do
|> LLMChain.run(opts)
end


@doc """
Runs the TextToTitleChain and evaluates the result to return the final answer.
If it was unable to generate a title, the `fallback_title` is returned.
Expand Down
34 changes: 23 additions & 11 deletions lib/chat_models/chat_ollama_ai.ex
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,13 @@ defmodule LangChain.ChatModels.ChatOllamaAI do
raise LangChainError, "Retries exceeded. Connection failed."
end

def do_api_request(%ChatOllamaAI{stream: false} = ollama_ai, messages, functions, callback_fn, retry_count) do
def do_api_request(
%ChatOllamaAI{stream: false} = ollama_ai,
messages,
functions,
callback_fn,
retry_count
) do
req =
Req.new(
url: ollama_ai.endpoint,
Expand All @@ -300,21 +306,27 @@ defmodule LangChain.ChatModels.ChatOllamaAI do
result
end

{:error, %Mint.TransportError{reason: :timeout}} ->
{:error, "Request timed out"}
{:error, %Mint.TransportError{reason: :timeout}} ->
{:error, "Request timed out"}

{:error, %Mint.TransportError{reason: :closed}} ->
# Force a retry by making a recursive call decrementing the counter
Logger.debug(fn -> "Mint connection closed: retry count = #{inspect(retry_count)}" end)
do_api_request(ollama_ai, messages, functions, callback_fn, retry_count - 1)
{:error, %Mint.TransportError{reason: :closed}} ->
# Force a retry by making a recursive call decrementing the counter
Logger.debug(fn -> "Mint connection closed: retry count = #{inspect(retry_count)}" end)
do_api_request(ollama_ai, messages, functions, callback_fn, retry_count - 1)

other ->
Logger.error("Unexpected and unhandled API response! #{inspect(other)}")
other
other ->
Logger.error("Unexpected and unhandled API response! #{inspect(other)}")
other
end
end

def do_api_request(%ChatOllamaAI{stream: true} = ollama_ai, messages, functions, callback_fn, retry_count) do
def do_api_request(
%ChatOllamaAI{stream: true} = ollama_ai,
messages,
functions,
callback_fn,
retry_count
) do
Req.new(
url: ollama_ai.endpoint,
json: for_api(ollama_ai, messages, functions),
Expand Down
6 changes: 4 additions & 2 deletions lib/utils/chain_result.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ defmodule LangChain.Utils.ChainResult do
This supports passing in the final, updated LLMChain, or the result of the
`LLMChain.run/2` function.
"""
@spec to_string(LLMChain.t() | {:ok, LLMChain.t(), Message.t()} | {:error, String.t()}) :: {:ok, String.t()} | {:error, String.t()}
@spec to_string(LLMChain.t() | {:ok, LLMChain.t(), Message.t()} | {:error, String.t()}) ::
{:ok, String.t()} | {:error, String.t()}
def to_string({:error, reason}) when is_binary(reason) do
# if an error was passed in, forward it through.
{:error, reason}
Expand Down Expand Up @@ -48,7 +49,8 @@ defmodule LangChain.Utils.ChainResult do
raises and exception with the reason why it cannot be used. See the docs for
`to_string/2` for details.
"""
@spec to_string!(LLMChain.t() | {:ok, LLMChain.t(), Message.t()} | {:error, String.t()}) :: String.t() | no_return()
@spec to_string!(LLMChain.t() | {:ok, LLMChain.t(), Message.t()} | {:error, String.t()}) ::
String.t() | no_return()
def to_string!(%LLMChain{} = chain) do
case ChainResult.to_string(chain) do
{:ok, result} -> result
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule LangChain.MixProject do
use Mix.Project

@version "0.1.7"
@version "0.1.8"

def project do
[
Expand Down
1 change: 1 addition & 0 deletions test/function_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ defmodule LangChain.FunctionTest do

%{functions: functions}
end

test "finds and returns function display text", %{functions: functions} do
assert "Speaking..." == Function.get_display_text(functions, "speak")
assert "Walking..." == Function.get_display_text(functions, "walk")
Expand Down
8 changes: 5 additions & 3 deletions test/prompt_template_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@ defmodule LangChain.PromptTemplateTest do
end

test "returns substitutions removed when replacement missing" do
{result, _} = with_io(:standard_error, fn ->
PromptTemplate.format_text("This is <%= @missing %>", %{other: "something"})
end)
{result, _} =
with_io(:standard_error, fn ->
PromptTemplate.format_text("This is <%= @missing %>", %{other: "something"})
end)

assert result == "This is "
end
end
Expand Down
8 changes: 5 additions & 3 deletions test/tools/calculator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ defmodule LangChain.Tools.CalculatorTest do
end

test "returns an error when evaluation fails" do
{result, _} = with_io(:standard_error, fn ->
Calculator.execute(%{"expression" => "cow + dog"}, nil)
end)
{result, _} =
with_io(:standard_error, fn ->
Calculator.execute(%{"expression" => "cow + dog"}, nil)
end)

assert "ERROR" == result
end
end
Expand Down

0 comments on commit 76ed163

Please sign in to comment.