Skip to content

Commit

Permalink
renamed namespace from Langchain to LangChain
Browse files Browse the repository at this point in the history
- changed the case
  • Loading branch information
brainlid committed Sep 18, 2023
1 parent 36e29c7 commit 5b39728
Show file tree
Hide file tree
Showing 30 changed files with 285 additions and 285 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"cSpell.words": [
"Langchain",
"LangChain",
"openai"
]
}
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,19 @@ config :langchain, openai_key: fn -> System.get_env("OPENAI_KEY") end

## Usage

The central module in this library is `Langchain.Chains.LLMChain`. Most other pieces are either inputs to this, or structures used by it. For understanding how to use the library, start there.
The central module in this library is `LangChain.Chains.LLMChain`. Most other pieces are either inputs to this, or structures used by it. For understanding how to use the library, start there.

### Exposing a custom Elixir function to ChatGPT

For an interactive example, refer to the project [Livebook notebook "Langchain: Executing Custom Elixir Functions"](notebooks/custom_functions.livemd).
For an interactive example, refer to the project [Livebook notebook "LangChain: Executing Custom Elixir Functions"](notebooks/custom_functions.livemd).

The following is an example of a function that receives parameter arguments.

```elixir
alias Langchain.Function
alias Langchain.Message
alias Langchain.Chains.LLMChain
alias Langchain.ChatModels.ChatOpenAI
alias LangChain.Function
alias LangChain.Message
alias LangChain.Chains.LLMChain
alias LangChain.ChatModels.ChatOpenAI

# map of data we want to be passed as `context` to the function when
# executed.
Expand Down Expand Up @@ -150,6 +150,6 @@ mix test

Executing a specific test, wether it is a `live_call` or not, will execute it creating a potentially billable event.

When doing local development on the `Langchain` library itself, rename the `.envrc_template` to `.envrc` and populate it with your private API values. This is only used when running live test when explicitly requested.
When doing local development on the `LangChain` library itself, rename the `.envrc_template` to `.envrc` and populate it with your private API values. This is only used when running live test when explicitly requested.

Use a tool like [Dotenv](https://github.com/motdotla/dotenv) to load the API values into the ENV when using the library locally.
14 changes: 7 additions & 7 deletions lib/chains/data_extraction_chain.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Langchain.Chains.DataExtractionChain do
defmodule LangChain.Chains.DataExtractionChain do
@moduledoc """
Defines an LLMChain for performing data extraction from a body of text.
Expand Down Expand Up @@ -32,7 +32,7 @@ defmodule Langchain.Chains.DataExtractionChain do
"Alex is 5 feet tall. Claudia is 4 feet taller than Alex and jumps higher than him.
Claudia is a brunette and Alex is blonde. Alex's dog Frosty is a labrador and likes to play hide and seek."
{:ok, result} = Langchain.Chains.DataExtractionChain.run(chat, schema_parameters, data_prompt)
{:ok, result} = LangChain.Chains.DataExtractionChain.run(chat, schema_parameters, data_prompt)
# Example result
[
Expand All @@ -54,9 +54,9 @@ defmodule Langchain.Chains.DataExtractionChain do
"""
use Ecto.Schema
require Logger
alias Langchain.PromptTemplate
alias Langchain.Message
alias Langchain.Chains.LLMChain
alias LangChain.PromptTemplate
alias LangChain.Message
alias LangChain.Chains.LLMChain

@extraction_template ~s"Extract and save the relevant entities mentioned in the following passage together with their properties.
Expand Down Expand Up @@ -109,9 +109,9 @@ defmodule Langchain.Chains.DataExtractionChain do
Build the function to expose to the LLM that can be called for data
extraction.
"""
@spec build_extract_function(json_schema :: map()) :: Langchain.Function.t() | no_return()
@spec build_extract_function(json_schema :: map()) :: LangChain.Function.t() | no_return()
def build_extract_function(json_schema) do
Langchain.Function.new!(%{
LangChain.Function.new!(%{
name: "information_extraction",
description: "Extracts the relevant information from the passage.",
parameters_schema: %{
Expand Down
14 changes: 7 additions & 7 deletions lib/chains/llm_chain.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Langchain.Chains.LLMChain do
defmodule LangChain.Chains.LLMChain do
@doc """
Define an LLMChain. This is the heart of the LangChain library.
Expand All @@ -14,12 +14,12 @@ defmodule Langchain.Chains.LLMChain do
use Ecto.Schema
import Ecto.Changeset
require Logger
alias Langchain.PromptTemplate
alias LangChain.PromptTemplate
alias __MODULE__
alias Langchain.Message
alias Langchain.MessageDelta
alias Langchain.Function
alias Langchain.LangchainError
alias LangChain.Message
alias LangChain.MessageDelta
alias LangChain.Function
alias LangChain.LangChainError

@primary_key false
embedded_schema do
Expand Down Expand Up @@ -81,7 +81,7 @@ defmodule Langchain.Chains.LLMChain do
chain

{:error, changeset} ->
raise LangchainError, changeset
raise LangChainError, changeset
end
end

Expand Down
44 changes: 22 additions & 22 deletions lib/chat_models/chat_open_ai.ex
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
defmodule Langchain.ChatModels.ChatOpenAI do
defmodule LangChain.ChatModels.ChatOpenAI do
@moduledoc """
Represents the [OpenAI ChatModel](https://platform.openai.com/docs/api-reference/chat/create).
Parses and validates inputs for making a requests from the OpenAI Chat API.
Converts responses into more specialized `Langchain` data structures.
Converts responses into more specialized `LangChain` data structures.
- https://github.com/openai/openai-cookbook/blob/main/examples/How_to_call_functions_with_chat_models.ipynb
"""
use Ecto.Schema
require Logger
import Ecto.Changeset
import Langchain.Utils.ApiOverride
import LangChain.Utils.ApiOverride
alias __MODULE__
alias Langchain.Config
alias Langchain.Message
alias Langchain.LangchainError
alias Langchain.ForOpenAIApi
alias Langchain.Utils
alias Langchain.MessageDelta
alias LangChain.Config
alias LangChain.Message
alias LangChain.LangChainError
alias LangChain.ForOpenAIApi
alias LangChain.Utils
alias LangChain.MessageDelta

# NOTE: As of gpt-4 and gpt-3.5, only one function_call is issued at a time
# even when multiple requests could be issued based on the prompt.
Expand Down Expand Up @@ -87,7 +87,7 @@ defmodule Langchain.ChatModels.ChatOpenAI do
chain

{:error, changeset} ->
raise LangchainError, changeset
raise LangChainError, changeset
end
end

Expand Down Expand Up @@ -133,19 +133,19 @@ defmodule Langchain.ChatModels.ChatOpenAI do
received from the API.
**NOTE:** This function *can* be used directly, but the primary interface
should be through `Langchain.Chains.LLMChain`. The `ChatOpenAI` module is more focused on
translating the `Langchain` data structures to and from the OpenAI API.
should be through `LangChain.Chains.LLMChain`. The `ChatOpenAI` module is more focused on
translating the `LangChain` data structures to and from the OpenAI API.
Another benefit of using `Langchain.Chains.LLMChain` is that it combines the
Another benefit of using `LangChain.Chains.LLMChain` is that it combines the
storage of messages, adding functions, adding custom context that should be
passed to functions, and automatically applying `Langchain.MessageDelta`
passed to functions, and automatically applying `LangChain.MessageDelta`
structs as they are are received, then converting those to the full
`Langchain.Message` once fully complete.
`LangChain.Message` once fully complete.
"""
@spec call(
t(),
String.t() | [Message.t()],
[Langchain.Function.t()],
[LangChain.Function.t()],
nil | (Message.t() | MessageDelta.t() -> any())
) :: call_response()
def call(openai, prompt, functions \\ [], callback_fn \\ nil)
Expand All @@ -170,7 +170,7 @@ defmodule Langchain.ChatModels.ChatOpenAI do
response

_other ->
raise LangchainError,
raise LangChainError,
"An unexpected fake API response was set. Should be an `{:ok, value}`"
end
else
Expand All @@ -184,7 +184,7 @@ defmodule Langchain.ChatModels.ChatOpenAI do
{:ok, parsed_data}
end
rescue
err in LangchainError ->
err in LangChainError ->
{:error, err.message}
end
end
Expand Down Expand Up @@ -258,7 +258,7 @@ defmodule Langchain.ChatModels.ChatOpenAI do
#
# body: [
# [
# %Langchain.MessageDelta{
# %LangChain.MessageDelta{
# content: nil,
# index: 0,
# function_name: nil,
Expand All @@ -279,7 +279,7 @@ defmodule Langchain.ChatModels.ChatOpenAI do
{request, response}

{:error, %Mint.TransportError{reason: :timeout}} ->
{request, LangchainError.exception("Request timed out")}
{request, LangChainError.exception("Request timed out")}

{:error, exception} ->
Logger.error("Failed request to API: #{inspect(exception)}")
Expand Down Expand Up @@ -308,7 +308,7 @@ defmodule Langchain.ChatModels.ChatOpenAI do
{:ok, %Req.Response{body: data}} ->
data

{:error, %LangchainError{message: reason}} ->
{:error, %LangChainError{message: reason}} ->
{:error, reason}

other ->
Expand Down Expand Up @@ -361,7 +361,7 @@ defmodule Langchain.ChatModels.ChatOpenAI do
# return the error
|> case do
[{:error, reason}] ->
raise LangchainError, reason
raise LangChainError, reason

other ->
other
Expand Down
2 changes: 1 addition & 1 deletion lib/config.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Langchain.Config do
defmodule LangChain.Config do
@moduledoc """
Utility that handles interaction with the application's configuration.
"""
Expand Down
6 changes: 3 additions & 3 deletions lib/for_open_ai_api.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defprotocol Langchain.ForOpenAIApi do
defprotocol LangChain.ForOpenAIApi do
@moduledoc """
A protocol that defines a way for converting the Langchain Elixir data structs
A protocol that defines a way for converting the LangChain Elixir data structs
to an OpenAI supported data structure and format for making an API call.
"""

Expand All @@ -12,6 +12,6 @@ defprotocol Langchain.ForOpenAIApi do
def for_api(struct)
end

defimpl Langchain.ForOpenAIApi, for: Any do
defimpl LangChain.ForOpenAIApi, for: Any do
def for_api(_struct), do: nil
end
20 changes: 10 additions & 10 deletions lib/function.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# {:ok, f} = Langchain.Function.new(%{name: "register_person", description: "Register a new person in the system", required: ["name"], parameters: [p_name, p_age]})
# {:ok, f} = LangChain.Function.new(%{name: "register_person", description: "Register a new person in the system", required: ["name"], parameters: [p_name, p_age]})
# NOTE: New in OpenAI - https://openai.com/blog/function-calling-and-other-api-updates
# - 13 June 2023
# NOTE: Pretty much takes the place of a Langchain "Tool".
defmodule Langchain.Function do
# NOTE: Pretty much takes the place of a LangChain "Tool".
defmodule LangChain.Function do
@moduledoc """
Defines a "function" that can be provided to an LLM for the LLM to optionally
execute and pass argument data to.
Expand All @@ -25,7 +25,7 @@ defmodule Langchain.Function do
This example defines a function that an LLM can execute for performing basic
math calculations. **NOTE:** This is a partial implementation of the
`Langchain.Tools.Calculator`.
`LangChain.Tools.Calculator`.
Function.new(%{
name: "calculator",
Expand All @@ -50,7 +50,7 @@ defmodule Langchain.Function do
map.
The `context` argument is passed through as the `context` on a
`Langchain.Chains.LLMChain`. This is whatever context data is needed for the
`LangChain.Chains.LLMChain`. This is whatever context data is needed for the
function to do it's work.
Context examples may be user_id, account_id, account struct, billing level,
Expand All @@ -66,7 +66,7 @@ defmodule Langchain.Function do
import Ecto.Changeset
require Logger
alias __MODULE__
alias Langchain.LangchainError
alias LangChain.LangChainError

@primary_key false
embedded_schema do
Expand Down Expand Up @@ -106,7 +106,7 @@ defmodule Langchain.Function do
function

{:error, changeset} ->
raise LangchainError, changeset
raise LangChainError, changeset
end
end

Expand All @@ -118,7 +118,7 @@ defmodule Langchain.Function do

@doc """
Execute the function passing in arguments and additional optional context.
This is called by a `Langchain.Chains.LLMChain` when a `Function` execution is
This is called by a `LangChain.Chains.LLMChain` when a `Function` execution is
requested by the LLM.
"""
def execute(%Function{function: fun} = function, arguments, context) do
Expand All @@ -127,8 +127,8 @@ defmodule Langchain.Function do
end
end

defimpl Langchain.ForOpenAIApi, for: Langchain.Function do
alias Langchain.Function
defimpl LangChain.ForOpenAIApi, for: LangChain.Function do
alias LangChain.Function

def for_api(%Function{} = fun) do
%{
Expand Down
4 changes: 2 additions & 2 deletions lib/gettext.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
defmodule Langchain.Gettext do
defmodule LangChain.Gettext do
@moduledoc """
A module providing Internationalization with a gettext-based API.
By using [Gettext](https://hexdocs.pm/gettext),
your module gains a set of macros for translations, for example:
import Langchain.Gettext
import LangChain.Gettext
# Simple translation
gettext("Here is the string to translate")
Expand Down
16 changes: 8 additions & 8 deletions lib/langchain_error.ex
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
defmodule Langchain.LangchainError do
defmodule LangChain.LangChainError do
@moduledoc """
Exception used for raising Langchain specific errors.
Exception used for raising LangChain specific errors.
It stores the `:message`. Passing an Ecto.Changeset with an error
converts the error into a string message.
raise LangchainError, changeset
raise LangChainError, changeset
raise LangchainError, "Message text"
raise LangChainError, "Message text"
"""
import Langchain.Utils, only: [changeset_error_to_string: 1]
import LangChain.Utils, only: [changeset_error_to_string: 1]
alias __MODULE__

@type t :: %LangchainError{}
@type t :: %LangChainError{}

defexception [:message]

Expand All @@ -23,11 +23,11 @@ defmodule Langchain.LangchainError do
"""
@spec exception(message :: String.t() | Ecto.Changeset.t()) :: t()
def exception(message) when is_binary(message) do
%LangchainError{message: message}
%LangChainError{message: message}
end

def exception(%Ecto.Changeset{} = changeset) do
text_reason = changeset_error_to_string(changeset)
%LangchainError{message: text_reason}
%LangChainError{message: text_reason}
end
end
Loading

0 comments on commit 5b39728

Please sign in to comment.