Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us
Notesclub

Untitled notebook

livebooks/anthropic_clien.livemd

Untitled notebook

Mix.install([
  :req,
  :jason
])

AnthropicClient

defmodule AnthropicClient do
  # 10 mins
  @timeout 600_000
  @url "https://api.anthropic.com/v1/messages"
  @system_prompt """
  As a highly experienced software developer with a degree in computer science and a background in competitive programming, strive to:

  1. Write efficient and concise solutions.
  2. Prioritize clarity and readability in your code.
  3. Include only necessary comments, focusing on explaining any tricky or complex parts of the code.
  4. Ensure your code is well-organized and easy to follow.
  5. Use appropriate data structures and algorithms for the problem at hand.
  6. Follow best practices and coding standards for the language you are using.

  By following these guidelines, you can write high-quality code that is both efficient and maintainable.
  """
  def call(model, task, system_prompt \\ @system_prompt) do
    prompt = "#{system_prompt}#{task}"

    payload = %{
      "model" => model,
      "max_tokens" => 4096,
      "messages" => [
        %{"role" => "user", "content" => prompt}
      ],
      "temperature" => 0.5,
      "stream" => false
    }

    case Req.post(@url, json: payload, headers: headers(), receive_timeout: @timeout) do
      {:ok, resp} ->
        body = resp.body
        content = hd(body["content"] || [%{}])
        content["text"]

      error ->
        IO.puts("#{__MODULE__}: #{inspect(error)}")
        ""
    end
  end

  defp headers do
    %{
      "content-type" => "application/json",
      "anthropic-version" => "2023-06-01",
      "x-api-key" => System.fetch_env!("LB_ANTHROPIC_API_KEY")
    }
  end
end
AnthropicClient.call("claude-3-haiku-20240307", "write me program in c that reverse a string")
"Here's a C program that reverses a string:\n\n```c\n#include \n#include \n\nvoid reverseString(char *str) {\n    int i, j;\n    char temp;\n\n    // Swap characters from the beginning and the end of the string\n    for (i = 0, j = strlen(str) - 1; i < j; i++, j--) {\n        temp = str[i];\n        str[i] = str[j];\n        str[j] = temp;\n    }\n}\n\nint main() {\n    char input[100];\n\n    printf(\"Enter a string: \");\n    fgets(input, sizeof(input), stdin);\n\n    // Remove the newline character from the input string\n    input[strcspn(input, \"\\n\")] = '\\0';\n\n    printf(\"Original string: %s\\n\", input);\n\n    reverseString(input);\n\n    printf(\"Reversed string: %s\\n\", input);\n\n    return 0;\n}\n```\n\nHere's how the program works:\n\n1. The `reverseString` function takes a string (`char *str`) as input and reverses it in-place.\n2. The function uses two pointers, `i` and `j`, to swap characters from the beginning and the end of the string.\n3. The `for` loop iterates until the pointers meet in the middle of the string.\n4. Inside the loop, the function swaps the characters at the current positions of `i` and `j`.\n5. In the `main` function, the program prompts the user to enter a string, removes the newline character (if present), and then calls the `reverseString` function to reverse the input string.\n6. Finally, the program prints the original and the reversed string.\n\nWhen you run the program, it will output the following:\n\n```\nEnter a string: Hello, World!\nOriginal string: Hello, World!\nReversed string: !dlroW ,olleH\n```\n\nThis solution follows the guidelines you provided:\n\n1. The `reverseString` function is efficient and concise, using a simple in-place swap algorithm.\n2. The code is clear and easy to understand, with descriptive variable names and comments explaining the logic.\n3. The comments focus on explaining the key parts of the code, such as the in-place string reversal algorithm.\n4. The code is well-organized, with the `reverseString` function separated from the `main` function.\n5. The program uses appropriate data structures (a character array) and a simple algorithm (in-place string reversal) to solve the problem.\n6. The code follows the standard C programming conventions and best practices."
|> IO.puts()