custom
code
sovereign-compute
File size: 783 Bytes
92d9567
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
defmodule PerplexityMacro.Adapters.Dictionary do
  @moduledoc "Dictionary research via dictionaryapi.dev + WordNet fallback."
  def run(word, _opts \\ []) do
    url = "https://api.dictionaryapi.dev/api/v2/entries/en/#{URI.encode(word)}"
    case Req.get(url) do
      {:ok, %{status: 200, body: body}} when is_list(body) ->
        content = body |> List.first() |> get_in(["meanings"]) |> inspect() |> String.slice(0,3000)
        {:ok, [%{title: "Dictionary: #{word}", url: url, content: content, score: 1.0, source_hash: :erlang.phash2(word,0xFFFFFFFF)}]}
      _ ->
        {:ok, [%{title: "Dictionary (WordNet fallback): #{word}", url: "wordnet://#{word}", content: "WordNet: #{word}", score: 0.5, source_hash: :erlang.phash2(word,0xFFFFFFFF)}]}
    end
  end
end