Skip to content

Commit

Permalink
Rename functions and improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
isavita committed Nov 19, 2023
1 parent 3cfa77e commit aa3cdf5
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 50 deletions.
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ WiseGPTEx is an Elixir library that utilizes OpenAI's GPT-3.5-turbo model and An
```elixir
def deps do
[
{:wise_gpt_ex, "~> 0.5.0"}
{:wise_gpt_ex, "~> 0.6.0"}
]
end
```
Expand All @@ -19,7 +19,7 @@ config :wise_gpt_ex, :openai_api_key, "your_openai_api_key"
config :wise_gpt_ex, :anthropic_api_key, "your_anthropic_api_key"
```

## Usage (OpenAI API)
## Usage (only with OpenAI API)

To use WiseGPTEx, simply call the `get_best_completion/2` or `get_best_completion_with_resolver/2` function with a question and optional list of options:
```elixir
Expand All @@ -32,23 +32,32 @@ opts = [model: "gpt-4", temperature: 0.4, num_completions: 4, timeout: 3_600_000
{:ok, response} = WiseGPTEx.get_best_completion_with_resolver("What is the capital of France?", opts)
```

You can also make a raw call to the OpenAI API using the `get_raw_completion/2` function:
You can also make a raw call to the OpenAI API using the `openai_completion/2` function:
```elixir
messages = [
%{"role" => "system", "content" => "You are High School Geography Teacher"},
%{"role" => "user", "content" => "What was the capital of France in 15th century?"}
]
{ok, response} = WiseGPTEx.get_raw_completion(messages, [model: "gpt-4", temperature: 0.75, timeout: 3_600])
{ok, response} = WiseGPTEx.openai_completion(messages, [model: "gpt-4", temperature: 0.75, timeout: 3_600])
```

Note that the `get_best_completion_with_resolver/2` function is similar to `get_best_completion/2`.
This is because the difference between these two functions is in the method of how they select the best completion, not in their usage or the nature of their inputs or outputs.
The `get_best_completion_with_resolver/2` function will perform an additional API call to get a more accurate completion, which can be beneficial for complex or ambiguous queries.
The `get_raw_completion/2` function allows sending a custom conversation to the API without any additional prompting or setting the up the system role etc. This is useful when you want direct access to the model's system messages.
The `openai_completion/2` function allows sending a custom conversation to the API without any additional prompting or setting the up the system role etc. This is useful when you want direct access to the model's system messages.

## OpenAI API
For interactions with the OpenAI API, use the `openai_completion/1` function:
```elixir
{:ok, response} = WiseGPTEx.openai_completion("What is the capital of France?")
{:ok, "Paris"}
```
## Anthropic API
For interactions with the Anthropic API, use the get_anthropic_completion/2 function:
{:ok, response} = WiseGPTEx.get_anthropic_completion("Why is the sky blue?")
For interactions with the Anthropic API, use the `anthropic_completion/1` function:
```elixir
{:ok, response} = WiseGPTEx.anthropic_completion("Why is the sky blue?")
{:ok, "The sky is blue because of the way sunlight interacts with Earth's atmosphere."}
```

## Options

Expand Down
42 changes: 10 additions & 32 deletions lib/wise_gpt_ex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,6 @@ defmodule WiseGPTEx do
This module provides functions to obtain the best completion from the OpenAI models (default: "gpt-3.5-turbo") using the OpenAI API completions endpoint (`https://api.openai.com/v1/chat/completions`).
The `get_best_completion/2` and `get_best_completion_with_resolver/2` functions take a question and an optional list of options to configure the API request. `get_best_completion_with_resolver/2` also involves a secondary step to resolve the best completion among the options, which leads to an additional API call for a more accurate response.
## Installation
1. Add `wise_gpt_ex` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:wise_gpt_ex, "~> 0.5.0"}
]
end
```
2. Add the OpenAI API key to your configuration file (e.g., config/config.exs):
```elixir
config :wise_gpt_ex, :openai_api_key, "your_openai_api_key"
```
3. Add the Anthropic API key to your configuration file (e.g., config/config.exs):
```elixir
config :wise_gpt_ex, :anthropic_api_key, "your_anthropic_api_key"
```
## Examples
Basic usage:
Expand All @@ -43,7 +21,7 @@ defmodule WiseGPTEx do
...> %{"role" => "system", "content" => "You are High School Geography Teacher"},
...> %{"role" => "user", "content" => "What was the capital of France in 15th century?"}
...> ]
...> WiseGPTEx.get_raw_completion(messages)
...> WiseGPTEx.openai_completion(messages)
{:ok, "The capital of France in the 15th century was Paris."}
Using all available options:
Expand All @@ -61,7 +39,7 @@ defmodule WiseGPTEx do
Anthropic API usage:
iex> WiseGPTEx.get_anthropic_completion("Why is the sky blue?")
iex> WiseGPTEx.anthropic_completion("Why is the sky blue?")
{:ok, "The sky is blue because... [detailed explanation]"}
## Options
Expand All @@ -73,7 +51,7 @@ defmodule WiseGPTEx do
* `:num_completions` - The number of completions to generate (default: 3).
* `:timeout` - The maximum time in milliseconds to wait for a response from the OpenAI API (default: 3_600_000 ms, or 60 minutes).
For `get_anthropic_completion/2`, the following options can be passed:
For `anthropic_completion/2`, the following options can be passed:
* `:model` - The version of the Claude model to use (default: "claude-2").
* `:temperature` - Controls the randomness of the model's output (default: 0.1).
Expand Down Expand Up @@ -111,13 +89,13 @@ defmodule WiseGPTEx do
...> %{"role" => "system", "content" => "You are High School Geography Teacher"},
...> %{"role" => "user", "content" => "What was the capital of France in 15th century?"}
...> ]
...> WiseGPTEx.get_raw_completion(messages)
...> WiseGPTEx.openai_completion(messages)
{:ok, "The capital of France in the 15th century was Paris."}
```
"""
@spec get_raw_completion(list(map()), Keyword.t()) :: {:ok, binary()} | {:error, any()}
def get_raw_completion(messages, opts \\ []) do
OpenAIHTTPClient.get_raw_completion(messages, opts)
@spec openai_completion(list(map()), Keyword.t()) :: {:ok, binary()} | {:error, any()}
def openai_completion(messages, opts \\ []) do
OpenAIHTTPClient.completion(messages, opts)
end

@doc """
Expand Down Expand Up @@ -248,12 +226,12 @@ defmodule WiseGPTEx do
## Example:
```elixir
iex> WiseGPTEx.get_anthropic_completion("Why is the sky blue?")
iex> WiseGPTEx.anthropic_completion("Why is the sky blue?")
{:ok, "The sky is blue because... [detailed explanation]"}
```
"""
@spec get_anthropic_completion(binary(), Keyword.t()) :: {:ok, binary()} | {:error, any()}
def get_anthropic_completion(message, opts \\ []) do
@spec anthropic_completion(binary(), Keyword.t()) :: {:ok, binary()} | {:error, any()}
def anthropic_completion(message, opts \\ []) do
AnthropicHTTPClient.complete(message, opts)
end
end
8 changes: 4 additions & 4 deletions lib/wise_gpt_ex/openai_http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ defmodule WiseGPTEx.OpenAIHTTPClient do
* `get_completions_with_reasoning/2` - Retrieves completions with reasoning from the OpenAI API based on a given message and an optional list of options.
* `get_best_completion/3` - Determines the best completion from a list of completions and an optional list of options.
* `get_raw_completion/2` - Gets a raw completion from the API based on given messages, without additional prompting.
* `completion/2` - Gets a raw completion from the API based on given messages, without additional prompting.
**NOTE:** `get_raw_completion/2` allows sending custom conversations with both role and content to the API, while the other functions add extra prompting to request reasoning, disambiguation, etc.
**NOTE:** `completion/2` allows sending custom conversations with both role and content to the API, while the other functions add extra prompting to request reasoning, disambiguation, etc.
"""
alias WiseGPTEx.OpenAIUtils, as: Utils

Expand All @@ -21,8 +21,8 @@ defmodule WiseGPTEx.OpenAIHTTPClient do
# 60 minutes
@default_timeout_ms 3_600_000

@spec get_raw_completion(list(map()), Keyword.t()) :: {:ok, binary()} | {:error, any()}
def get_raw_completion(messages, opts \\ []) do
@spec completion(list(map()), Keyword.t()) :: {:ok, binary()} | {:error, any()}
def completion(messages, opts \\ []) do
model = Keyword.get(opts, :model, @default_model)
temperature = Keyword.get(opts, :temperature, @default_temperature)
timeout = Keyword.get(opts, :timeout, @default_timeout_ms)
Expand Down
Binary file added logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 13 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule WiseGPTEx.MixProject do
use Mix.Project

@version "0.5.0"
@version "0.6.0"
@docs_url "http://hexdocs.pm/wise_gpt_ex"
@github_url "https://github.com/isavita/wise_gpt_ex"

Expand All @@ -15,7 +15,8 @@ defmodule WiseGPTEx.MixProject do
package: package(),
description: description(),
name: "WiseGPTEx",
source_url: @github_url
source_url: @github_url,
docs: docs()
]
end

Expand Down Expand Up @@ -52,4 +53,14 @@ defmodule WiseGPTEx.MixProject do
}
]
end

defp docs do
[
main: "readme",
logo: "logo.png",
source_url: @github_url,
source_ref: "v#{@version}",
extras: ["README.md"]
]
end
end
4 changes: 2 additions & 2 deletions test/wise_gpt_ex/openai_http_client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule WiseGPTEx.OpenAIHTTPClientTest do
import Mock
alias WiseGPTEx.OpenAIHTTPClient

describe "get_raw_completion/1" do
describe "openai_completion/1" do
@success_resp %{
status_code: 200,
body:
Expand All @@ -30,7 +30,7 @@ defmodule WiseGPTEx.OpenAIHTTPClientTest do

@success_resp
end do
assert {:ok, completion} = OpenAIHTTPClient.get_raw_completion(messages)
assert {:ok, completion} = OpenAIHTTPClient.completion(messages)
assert completion == "The capital of France in the 15th century was Paris."
end
end
Expand Down
6 changes: 3 additions & 3 deletions test/wise_gpt_ex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ defmodule WiseGPTExTest do
end
end

describe "get_anthropic_completion/2" do
describe "anthropic_completion/2" do
@anthropic_success_resp %{
status_code: 200,
body:
Expand All @@ -102,7 +102,7 @@ defmodule WiseGPTExTest do
end do
assert {:ok,
"The sky is blue because of the way sunlight interacts with Earth's atmosphere."} =
WiseGPTEx.get_anthropic_completion("Why is the sky blue?")
WiseGPTEx.anthropic_completion("Why is the sky blue?")
end
end

Expand All @@ -122,7 +122,7 @@ defmodule WiseGPTExTest do
"message" => "Unauthorized access",
"type" => "authentication_error"
}
}} = WiseGPTEx.get_anthropic_completion("Why is the sky blue?")
}} = WiseGPTEx.anthropic_completion("Why is the sky blue?")
end
end
end
Expand Down

0 comments on commit aa3cdf5

Please sign in to comment.