-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5deb5c6
commit e9ed1bc
Showing
7 changed files
with
286 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
87 changes: 87 additions & 0 deletions
87
src/phoenix/server/api/helpers/prompts/conversions/anthropic.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Optional, Union | ||
|
||
from typing_extensions import assert_never | ||
|
||
if TYPE_CHECKING: | ||
from anthropic.types import ( | ||
ToolChoiceAnyParam, | ||
ToolChoiceAutoParam, | ||
ToolChoiceParam, | ||
ToolChoiceToolParam, | ||
) | ||
|
||
from phoenix.server.api.helpers.prompts.models import ( | ||
PromptToolChoiceOneOrMore, | ||
PromptToolChoiceSpecificFunctionTool, | ||
PromptToolChoiceZeroOrMore, | ||
) | ||
|
||
|
||
class AnthropicToolChoiceConversion: | ||
@staticmethod | ||
def to_anthropic( | ||
obj: Union[ | ||
PromptToolChoiceZeroOrMore, | ||
PromptToolChoiceOneOrMore, | ||
PromptToolChoiceSpecificFunctionTool, | ||
], | ||
disable_parallel_tool_use: Optional[bool] = None, | ||
) -> ToolChoiceParam: | ||
if obj.type == "zero-or-more": | ||
choice_auto: ToolChoiceAutoParam = {"type": "auto"} | ||
if disable_parallel_tool_use is not None: | ||
choice_auto["disable_parallel_tool_use"] = disable_parallel_tool_use | ||
return choice_auto | ||
if obj.type == "one-or-more": | ||
choice_any: ToolChoiceAnyParam = {"type": "any"} | ||
if disable_parallel_tool_use is not None: | ||
choice_any["disable_parallel_tool_use"] = disable_parallel_tool_use | ||
return choice_any | ||
if obj.type == "specific-function-tool": | ||
choice_tool: ToolChoiceToolParam = {"type": "tool", "name": obj.function_name} | ||
if disable_parallel_tool_use is not None: | ||
choice_tool["disable_parallel_tool_use"] = disable_parallel_tool_use | ||
return choice_tool | ||
assert_never(obj.type) | ||
|
||
@staticmethod | ||
def from_anthropic( | ||
obj: ToolChoiceParam, | ||
) -> tuple[ | ||
Union[ | ||
PromptToolChoiceZeroOrMore, | ||
PromptToolChoiceOneOrMore, | ||
PromptToolChoiceSpecificFunctionTool, | ||
], | ||
Optional[bool], | ||
]: | ||
from phoenix.server.api.helpers.prompts.models import ( | ||
PromptToolChoiceOneOrMore, | ||
PromptToolChoiceSpecificFunctionTool, | ||
PromptToolChoiceZeroOrMore, | ||
) | ||
|
||
if obj["type"] == "auto": | ||
disable_parallel_tool_use = ( | ||
obj["disable_parallel_tool_use"] if "disable_parallel_tool_use" in obj else None | ||
) | ||
choice_zero_or_more = PromptToolChoiceZeroOrMore(type="zero-or-more") | ||
return choice_zero_or_more, disable_parallel_tool_use | ||
if obj["type"] == "any": | ||
disable_parallel_tool_use = ( | ||
obj["disable_parallel_tool_use"] if "disable_parallel_tool_use" in obj else None | ||
) | ||
choice_one_or_more = PromptToolChoiceOneOrMore(type="one-or-more") | ||
return choice_one_or_more, disable_parallel_tool_use | ||
if obj["type"] == "tool": | ||
disable_parallel_tool_use = ( | ||
obj["disable_parallel_tool_use"] if "disable_parallel_tool_use" in obj else None | ||
) | ||
choice_function_tool = PromptToolChoiceSpecificFunctionTool( | ||
type="specific-function-tool", | ||
function_name=obj["name"], | ||
) | ||
return choice_function_tool, disable_parallel_tool_use | ||
assert_never(obj) |
78 changes: 78 additions & 0 deletions
78
src/phoenix/server/api/helpers/prompts/conversions/openai.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Union | ||
|
||
from typing_extensions import assert_never | ||
|
||
if TYPE_CHECKING: | ||
from openai.types.chat import ( | ||
ChatCompletionNamedToolChoiceParam, | ||
ChatCompletionToolChoiceOptionParam, | ||
) | ||
from openai.types.chat.chat_completion_named_tool_choice_param import Function | ||
|
||
from phoenix.server.api.helpers.prompts.models import ( | ||
PromptToolChoiceNone, | ||
PromptToolChoiceOneOrMore, | ||
PromptToolChoiceSpecificFunctionTool, | ||
PromptToolChoiceZeroOrMore, | ||
) | ||
|
||
|
||
class OpenAIToolChoiceConversion: | ||
@staticmethod | ||
def to_openai( | ||
obj: Union[ | ||
PromptToolChoiceNone, | ||
PromptToolChoiceZeroOrMore, | ||
PromptToolChoiceOneOrMore, | ||
PromptToolChoiceSpecificFunctionTool, | ||
], | ||
) -> ChatCompletionToolChoiceOptionParam: | ||
if obj.type == "none": | ||
return "none" | ||
if obj.type == "zero-or-more": | ||
return "auto" | ||
if obj.type == "one-or-more": | ||
return "required" | ||
if obj.type == "specific-function-tool": | ||
choice_tool: ChatCompletionNamedToolChoiceParam = { | ||
"type": "function", | ||
"function": {"name": obj.function_name}, | ||
} | ||
return choice_tool | ||
assert_never(obj) | ||
|
||
@staticmethod | ||
def from_openai( | ||
obj: ChatCompletionToolChoiceOptionParam, | ||
) -> Union[ | ||
PromptToolChoiceNone, | ||
PromptToolChoiceZeroOrMore, | ||
PromptToolChoiceOneOrMore, | ||
PromptToolChoiceSpecificFunctionTool, | ||
]: | ||
from phoenix.server.api.helpers.prompts.models import ( | ||
PromptToolChoiceNone, | ||
PromptToolChoiceOneOrMore, | ||
PromptToolChoiceSpecificFunctionTool, | ||
PromptToolChoiceZeroOrMore, | ||
) | ||
|
||
if obj == "none": | ||
choice_none = PromptToolChoiceNone(type="none") | ||
return choice_none | ||
if obj == "auto": | ||
choice_zero_or_more = PromptToolChoiceZeroOrMore(type="zero-or-more") | ||
return choice_zero_or_more | ||
if obj == "required": | ||
choice_one_or_more = PromptToolChoiceOneOrMore(type="one-or-more") | ||
return choice_one_or_more | ||
if obj["type"] == "function": | ||
function: Function = obj["function"] | ||
choice_function_tool = PromptToolChoiceSpecificFunctionTool( | ||
type="specific-function-tool", | ||
function_name=function["name"], | ||
) | ||
return choice_function_tool | ||
assert_never(obj["type"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters