-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Codex Integrations] Add lanchain + aws integrations (#17)
- Loading branch information
Showing
8 changed files
with
195 additions
and
15 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,17 @@ | ||
from cleanlab_codex.utils.aws import Tool as AWSConverseTool | ||
from cleanlab_codex.utils.aws import ToolSpec as AWSToolSpec | ||
from cleanlab_codex.utils.aws import format_as_aws_converse_tool | ||
from cleanlab_codex.utils.openai import Function as OpenAIFunction | ||
from cleanlab_codex.utils.openai import FunctionParameters as OpenAIFunctionParameters | ||
from cleanlab_codex.utils.openai import Tool as OpenAITool | ||
from cleanlab_codex.utils.openai import format_as_openai_tool | ||
from cleanlab_codex.utils.types import FunctionParameters | ||
|
||
__all__ = ["OpenAIFunction", "OpenAIFunctionParameters", "OpenAITool", "format_as_openai_tool"] | ||
__all__ = [ | ||
"FunctionParameters", | ||
"OpenAIFunction", | ||
"OpenAITool", | ||
"AWSConverseTool", | ||
"AWSToolSpec", | ||
"format_as_openai_tool", | ||
"format_as_aws_converse_tool", | ||
] |
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,32 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any, Dict, List | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from cleanlab_codex.utils.types import FunctionParameters | ||
|
||
|
||
class ToolSpec(BaseModel): | ||
name: str | ||
description: str | ||
input_schema: Dict[str, FunctionParameters] = Field(alias="inputSchema") | ||
|
||
|
||
class Tool(BaseModel): | ||
tool_spec: ToolSpec = Field(alias="toolSpec") | ||
|
||
|
||
def format_as_aws_converse_tool( | ||
tool_name: str, | ||
tool_description: str, | ||
tool_properties: Dict[str, Any], | ||
required_properties: List[str], | ||
) -> Dict[str, Any]: | ||
return Tool( | ||
toolSpec=ToolSpec( | ||
name=tool_name, | ||
description=tool_description, | ||
inputSchema={"json": FunctionParameters(properties=tool_properties, required=required_properties)}, | ||
) | ||
).model_dump(by_alias=True) |
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,44 @@ | ||
from __future__ import annotations | ||
|
||
from inspect import signature | ||
from typing import Any, Callable, Dict | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
|
||
def create_args_schema(name: str, func: Callable[..., Any], tool_properties: Dict[str, Any]) -> type[BaseModel]: | ||
""" | ||
Creates a pydantic BaseModel for langchain's args_schema. | ||
Args: | ||
name: Name of the schema. | ||
func: The function for which the schema is being generated. | ||
tool_properties: Metadata about each argument. | ||
Returns: | ||
type[BaseModel]: A pydantic model, annotated as required by langchain. | ||
""" | ||
fields = {} | ||
params = signature(func).parameters | ||
|
||
for param_name, param in params.items(): | ||
param_type = param.annotation if param.annotation is not param.empty else Any | ||
param_default = param.default | ||
description = tool_properties.get(param_name, {}).get("description", None) | ||
|
||
if param_default is param.empty: | ||
fields[param_name] = (param_type, Field(description=description)) | ||
else: | ||
fields[param_name] = ( | ||
param_type, | ||
Field(default=param_default, description=description), | ||
) | ||
|
||
return type( | ||
name, | ||
(BaseModel,), | ||
{ | ||
"__annotations__": {k: v[0] for k, v in fields.items()}, | ||
**{k: v[1] for k, v in fields.items()}, | ||
}, | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from typing import Dict, List, Literal | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class Property(BaseModel): | ||
type: Literal["string", "number", "integer", "boolean", "array", "object"] | ||
description: str | ||
|
||
|
||
class FunctionParameters(BaseModel): | ||
type: Literal["object"] = "object" | ||
properties: Dict[str, Property] | ||
required: List[str] |
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