Skip to content

Bedrock Support #147

@alexdont

Description

@alexdont

I am working on this project, and I want to use bedrock as I my chat service of choice, I tested this library with openai chatgpt-4, and it works perfectly.

Here is the code I am testing to connect to the aws bedrock api.

Mix.install([
  {:langchain, "~> 0.3.0-rc.0"},
  {:kino, "~> 0.12.0"}
])

# Set the OpenAI API key
Application.put_env(
  :langchain,
  :openai_key,
  {MY_BEDROCK_API_KEY_GOES_HERE}
)

alias LangChain.Chains.LLMChain
alias LangChain.ChatModels.ChatOpenAI
alias LangChain.Message
alias LangChain.MessageDelta

defmodule Chatbot do
  def start do
    handler = %{
      on_llm_new_delta: fn _model, %MessageDelta{} = data ->
        IO.write(data.content)
      end,
      on_message_processed: fn _chain, %Message{} = data ->
        IO.puts("")
        IO.puts("")
        IO.inspect(data.content, label: "COMPLETED MESSAGE")
      end
    }

    chain =
      %{
        llm:
          ChatOpenAI.new!(%{
            model: "amazon.titan-text-express-v1",
            stream: true,
            callbacks: [handler],
            endpoint:
              "https://bedrock-runtime.us-east-1.amazonaws.com/model/amazon.titan-text-express-v1/invoke"
          }),
        callbacks: [handler]
      }
      |> LLMChain.new!()
      |> LLMChain.add_message(
        Message.new_system!(
          "You are a helpful assistant. Provide concise and accurate responses to user queries."
        )
      )

    chat_loop(chain)
  end

  defp chat_loop(chain) do
    user_input = IO.gets("You: ") |> String.trim()

    if user_input == "exit" do
      IO.puts("Chatbot: Goodbye!")
    else
      {:ok, updated_chain, _response} =
        chain
        |> LLMChain.add_message(Message.new_user!(user_input))
        |> LLMChain.run()

      chat_loop(updated_chain)
    end
  end
end

# Start the chatbot
Chatbot.start()

Can someone help me out, what am I doing wrong? or bedrock isn't supported yet?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions