9
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ElixirでDifyを使う

Last updated at Posted at 2024-12-15

前提

Difyにログインできること

Dify側のワークフローを作る

開始にin変数を作る
image.png

LLMにSYSTEMプロンプトを入力
プロンプトの中に開始のin変数を含める
image.png

終了に出力変数textを作る
image.png

APIキー取得方法

画面の赤矢印のように操作してください

image.png

image.png

image.png

image.png

Elixir側

ポイント Authorization:にAPIキーを設定
あとは、Difyの開始にin変数を作ると同じ変数を
{"in": "#{str}"}をjsonとして指定する

lib/data/lib.ex
defmodule Data.Lib do
  def llm(str) do
    headers = [
      "Content-Type": "application/json",
      Authorization: "Bearer app-xxxxxxxxxxxxxxxxxxxxxxxx"
    ]

    json = """
    {
      "inputs": {"in": "#{str}"},
      "response_mode": "blocking",
      "user": "abc-123"
    }
    """

    "https://api.dify.ai/v1/workflows/run"
    |> Req.post!(headers: headers, body: json)
    |> Map.get(:body)
    |> Map.get("data")
    |> Map.get("outputs")
    |> Map.get("text")
    |> IO.puts()
  end
end

reqをdepsに設定

mix.exs
# 省略
  defp deps do
    [
      {:req, "~> 0.5.8"}
    ]
  end
# 省略

実行結果

image.png

9
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?