Packages
langchain
0.4.0-rc.2
0.9.2
0.9.1
0.9.0
0.8.14
0.8.13
0.8.12
0.8.11
0.8.10
0.8.9
0.8.8
0.8.7
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.0
0.6.3
0.6.2
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.4.0-rc.3
0.4.0-rc.2
0.4.0-rc.1
0.4.0-rc.0
0.3.3
0.3.2
0.3.1
0.3.0
0.3.0-rc.2
0.3.0-rc.1
0.3.0-rc.0
0.2.0
0.1.10
0.1.9
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
Elixir implementation of a LangChain style framework that lets Elixir projects integrate with and leverage LLMs.
Current section
Files
Jump to
Current section
Files
lib/utils/parser/llama3_1_custom_tool_parser.ex
if Code.ensure_loaded?(NimbleParsec) do
defmodule LangChain.Utils.Parser.LLAMA_3_1_CustomToolParser do
import NimbleParsec
# Core components
start_tag = string("<function=")
end_tag = string("</function>")
# Function name: letters, numbers, and underscores
function_name =
ascii_string([?a..?z, ?A..?Z, ?0..?9, ?_], min: 1)
|> unwrap_and_tag(:function_name)
# Parameters: capture everything between > and </function> as JSON
parameters =
ignore(string(">"))
|> ascii_string([not: ?<], min: 0)
|> map({Jason, :decode!, []})
|> unwrap_and_tag(:parameters)
# Complete function call parser
defparsec(
:parse_function_call,
ignore(start_tag)
|> concat(function_name)
|> concat(parameters)
|> ignore(end_tag)
|> eos()
)
# Helper function for easier parsing
def parse(input) when is_binary(input) do
try do
case parse_function_call(input) do
{:ok, result, "", _, _, _} -> {:ok, Map.new(result)}
{:error, reason, _, _, _, _} -> {:error, reason}
end
rescue
Jason.DecodeError -> {:error, "Invalid JSON in parameters"}
end
end
end
end