Packages
langchain
0.9.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/message/citation.ex
defmodule LangChain.Message.Citation do
@moduledoc """
Represents a citation linking a span of response text to a source.
All four major providers (Anthropic, OpenAI, Google, Perplexity) share the
pattern of linking response text to sources. This struct normalizes those
provider-specific formats into a common shape.
## Fields
- `:cited_text` - The actual text cited from the source (when available)
- `:source` - A `CitationSource` identifying where the citation came from
- `:start_index` - Character offset into the ContentPart's text where this
citation starts
- `:end_index` - Character offset where this citation ends
- `:confidence` - Confidence score (0.0-1.0) when provided (e.g., Gemini)
- `:metadata` - Provider-specific data for round-tripping. String keys for
JSON serialization.
"""
use Ecto.Schema
import Ecto.Changeset
alias LangChain.LangChainError
alias LangChain.Message.CitationSource
alias __MODULE__
@primary_key false
embedded_schema do
field :cited_text, :string
embeds_one :source, CitationSource
field :start_index, :integer
field :end_index, :integer
field :confidence, :float
# Provider-specific data. String keys for JSON serialization.
field :metadata, :map, default: %{}
end
@type t :: %Citation{}
@create_fields [:cited_text, :start_index, :end_index, :confidence, :metadata]
@doc false
def changeset(citation, attrs) do
citation
|> cast(attrs, @create_fields)
|> cast_embed(:source)
end
@spec new(map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
def new(attrs \\ %{}) do
%Citation{}
|> changeset(attrs)
|> apply_action(:insert)
end
@spec new!(map()) :: t() | no_return()
def new!(attrs \\ %{}) do
case new(attrs) do
{:ok, struct} -> struct
{:error, changeset} -> raise LangChainError, changeset
end
end
@doc """
Returns all unique source URLs from a list of citations.
"""
@spec source_urls([t()]) :: [String.t()]
def source_urls(citations) when is_list(citations) do
citations
|> Enum.map(& &1.source.url)
|> Enum.reject(&is_nil/1)
|> Enum.uniq()
end
@doc """
Filters citations by source type.
"""
@spec filter_by_source_type([t()], atom()) :: [t()]
def filter_by_source_type(citations, type) when is_list(citations) do
Enum.filter(citations, &(&1.source && &1.source.type == type))
end
end