Skip to content

Implement stop_reason #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/llm/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ def logprobs
@extra[:logprobs]
end

##
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we link to some relevant docs - similar to logprobs ? Although there's more providers to cover this time

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Thanks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0x1eef should we just link to stop_reason from OpenAI? or link all of them? There's probably no generic docs for it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we could link to the relevant docs for each I think that'd be helpful - failing that, let's just reference OpenAI

# Returns the stop reason
# @return [String]
def stop_reason
@extra[:stop_reason]
end

def to_h
{role:, content:}
end
Expand Down
3 changes: 2 additions & 1 deletion lib/llm/providers/anthropic/response_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def parse_completion(raw)
{
model: raw["model"],
choices: raw["content"].map do
LLM::Message.new(raw["role"], _1["text"])
stop_reason = raw["stop_reason"]
LLM::Message.new(raw["role"], _1["text"], {stop_reason:})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we write this as:

{stop_reason: _1["stop_reason"]}

end,
prompt_tokens: raw.dig("usage", "input_tokens"),
completion_tokens: raw.dig("usage", "output_tokens")
Expand Down
4 changes: 3 additions & 1 deletion lib/llm/providers/gemini/response_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ def parse_completion(raw)
{
model: raw["modelVersion"],
choices: raw["candidates"].map do
stop_reason = _1["finishReason"]
LLM::Message.new(
_1.dig("content", "role"),
{text: _1.dig("content", "parts", 0, "text")}
{text: _1.dig("content", "parts", 0, "text")},
{stop_reason:}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we write this as

{stop_reason: _1["stop_reason"]}

)
end,
prompt_tokens: raw.dig("usageMetadata", "promptTokenCount"),
Expand Down
2 changes: 1 addition & 1 deletion lib/llm/providers/ollama/response_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module ResponseParser
def parse_completion(raw)
{
model: raw["model"],
choices: [LLM::Message.new(*raw["message"].values_at("role", "content"))],
choices: [LLM::Message.new(*raw["message"].values_at("role", "content"), {stop_reason: raw["done_reason"]})],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

prompt_tokens: raw.dig("prompt_eval_count"),
completion_tokens: raw.dig("eval_count")
}
Expand Down
4 changes: 3 additions & 1 deletion lib/llm/providers/openai/response_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def parse_completion(raw)
{
model: raw["model"],
choices: raw["choices"].map do
LLM::Message.new(*_1["message"].values_at("role", "content"), {logprobs: _1["logprobs"]})
logprobs = _1["logprobs"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be:

logprobs, stop_reason = _1.values_at("logprobs", "finish_reason")

stop_reason = _1["finish_reason"]
LLM::Message.new(*_1["message"].values_at("role", "content"), {logprobs:, stop_reason:})
end,
prompt_tokens: raw.dig("usage", "prompt_tokens"),
completion_tokens: raw.dig("usage", "completion_tokens"),
Expand Down
4 changes: 4 additions & 0 deletions spec/anthropic/completion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@
total_tokens: 2598
)
end

it "has stop reason" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it "includes a stop reason" do
  #..

expect(completion.choices.first.stop_reason).to eq("end_turn")
end
end

context "with an unauthorized error", :unauthorized do
Expand Down
4 changes: 4 additions & 0 deletions spec/gemini/completion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@
total_tokens: 12
)
end

it "has stop_reason" do
expect(completion.choices.first.stop_reason).to eq("STOP")
end
end

context "with an unauthorized error", :unauthorized do
Expand Down
5 changes: 5 additions & 0 deletions spec/ollama/completion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"content": "Hello! How are you today?"
},
"done": true,
"done_reason": "stop",
"total_duration": 5191566416,
"load_duration": 2154458,
"prompt_eval_count": 26,
Expand Down Expand Up @@ -87,6 +88,10 @@
total_tokens: 324
)
end

it "has stop reason for first choice" do
expect(completion.choices.first.stop_reason).to eq("stop")
end
end

# context "with an unauthorized error", :unauthorized do
Expand Down
4 changes: 4 additions & 0 deletions spec/openai/completion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@
total_tokens: 18
)
end

it "has stop reason" do
expect(completion.choices.first.stop_reason).to eq("stop")
end
end

context "with an unauthorized error", :unauthorized do
Expand Down