defmodule Prism.NPI.Lookup do
use Ash.Resource, extensions: [AshAi], domain: Prism.NPI
alias LangChain.Chains.LLMChain
alias LangChain.Message
alias LangChain.NativeTool
alias LangChain.ChatModels.ChatGoogleAI
alias LangChain.MessageProcessors.JsonProcessor
actions do
action :find_person, Prism.NPI.Person do
description "Look up a person's NPI information for a given query phrase (intended to be a name)"
argument :query, :string do
allow_nil? false
end
run fn input, _context ->
model = ChatGoogleAI.new!(%{temperature: 0, stream: false, model: "gemini-2.0-flash"})
query = input.arguments.query
{:ok, updated_chain} =
%{llm: model, verbose: false, stream: false}
|> LLMChain.new!()
|> LLMChain.add_message(
Message.new_user!("""
Search for NPPES NPI results for the following physician: #{query}
NPPES NPI information is primarily found on the following website: https://npiregistry.cms.hhs.gov
IMPORTANT INSTRUCTIONS:
You MUST respond with valid JSON that matches the following schema:
``json
{
"id": "1234567890",
"last_name": "DOE",
"first_name": "JOHN",
"primary_fax_number": "+14075981501",
"taxonomy": ["taxonomy"]
}
``
""")
)
|> LLMChain.add_tools(NativeTool.new!(%{name: "google_search", configuration: %{}}))
|> LLMChain.message_processors([JsonProcessor.new!(~r/``json(.*?)``/s)])
|> LLMChain.run()
case updated_chain do
%{last_message: %{processed_content: json}} ->
Ash.Type.cast_input(Prism.NPI.Person, json)
_ ->
{:ok, nil}
end
end
end
end
end
defmodule Prism.NPI.Lookup do
use Ash.Resource, extensions: [AshAi], domain: Prism.NPI
alias LangChain.Chains.LLMChain
alias LangChain.Message
alias LangChain.NativeTool
alias LangChain.ChatModels.ChatGoogleAI
alias LangChain.MessageProcessors.JsonProcessor
actions do
action :find_person, Prism.NPI.Person do
description "Look up a person's NPI information for a given query phrase (intended to be a name)"
argument :query, :string do
allow_nil? false
end
run fn input, _context ->
model = ChatGoogleAI.new!(%{temperature: 0, stream: false, model: "gemini-2.0-flash"})
query = input.arguments.query
{:ok, updated_chain} =
%{llm: model, verbose: false, stream: false}
|> LLMChain.new!()
|> LLMChain.add_message(
Message.new_user!("""
Search for NPPES NPI results for the following physician: #{query}
NPPES NPI information is primarily found on the following website: https://npiregistry.cms.hhs.gov
IMPORTANT INSTRUCTIONS:
You MUST respond with valid JSON that matches the following schema:
``json
{
"id": "1234567890",
"last_name": "DOE",
"first_name": "JOHN",
"primary_fax_number": "+14075981501",
"taxonomy": ["taxonomy"]
}
``
""")
)
|> LLMChain.add_tools(NativeTool.new!(%{name: "google_search", configuration: %{}}))
|> LLMChain.message_processors([JsonProcessor.new!(~r/``json(.*?)``/s)])
|> LLMChain.run()
case updated_chain do
%{last_message: %{processed_content: json}} ->
Ash.Type.cast_input(Prism.NPI.Person, json)
_ ->
{:ok, nil}
end
end
end
end
end