Skip to content

Commit

Permalink
Modify ChatAgent function call args json and make slack sdk optional (
Browse files Browse the repository at this point in the history
  • Loading branch information
zechengz authored Jun 20, 2024
1 parent b481c72 commit 28621b5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
4 changes: 2 additions & 2 deletions camel/agents/chat_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ def step_tool_call(
func = self.func_dict[func_name]

args_str: str = choice.message.tool_calls[0].function.arguments
args = json.loads(args_str.replace("'", "\""))
args = json.loads(args_str)

# Pass the extracted arguments to the indicated function
try:
Expand Down Expand Up @@ -702,7 +702,7 @@ async def step_tool_call_async(
func = self.func_dict[func_name]

args_str: str = choice.message.tool_calls[0].function.arguments
args = json.loads(args_str.replace("'", "\""))
args = json.loads(args_str)

# Pass the extracted arguments to the indicated function
try:
Expand Down
15 changes: 11 additions & 4 deletions camel/functions/open_api_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
import json
import os
from typing import Any, Callable, Dict, List, Tuple
from typing import Any, Callable, Dict, List, Optional, Tuple

import prance
import requests

from camel.functions import OpenAIFunction, openapi_security_config
from camel.types import OpenAPIName


def parse_openapi_file(openapi_spec_path: str) -> Dict[str, Any]:
def parse_openapi_file(openapi_spec_path: str) -> Optional[Dict[str, Any]]:
r"""Load and parse an OpenAPI specification file.
This function utilizes the `prance.ResolvingParser` to parse and resolve
Expand All @@ -34,8 +33,14 @@ def parse_openapi_file(openapi_spec_path: str) -> Dict[str, Any]:
specification.
Returns:
Dict[str, Any]: The parsed OpenAPI specification as a dictionary.
Optional[Dict[str, Any]]: The parsed OpenAPI specification
as a dictionary. :obj:`None` if the package is not installed.
"""
try:
import prance
except Exception:
return None

# Load the OpenAPI spec
parser = prance.ResolvingParser(
openapi_spec_path, backend="openapi-spec-validator", strict=False
Expand Down Expand Up @@ -451,6 +456,8 @@ def apinames_filepaths_to_funs_schemas(
)

openapi_spec = parse_openapi_file(file_path)
if openapi_spec is None:
return [], []

# Generate and merge function schemas
openapi_functions_schemas = openapi_spec_to_openai_schemas(
Expand Down
16 changes: 14 additions & 2 deletions camel/functions/slack_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

from slack_sdk import WebClient

from slack_sdk.errors import SlackApiError

from camel.functions import OpenAIFunction

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -89,6 +87,8 @@ def create_slack_channel(name: str, is_private: Optional[bool] = True) -> str:
SlackApiError: If there is an error during get slack channel
information.
"""
from slack_sdk.errors import SlackApiError

try:
slack_client = _login_slack()
response = slack_client.conversations_create(
Expand All @@ -115,6 +115,8 @@ def join_slack_channel(channel_id: str) -> str:
SlackApiError: If there is an error during get slack channel
information.
"""
from slack_sdk.errors import SlackApiError

try:
slack_client = _login_slack()
response = slack_client.conversations_join(channel=channel_id)
Expand All @@ -137,6 +139,8 @@ def leave_slack_channel(channel_id: str) -> str:
SlackApiError: If there is an error during get slack channel
information.
"""
from slack_sdk.errors import SlackApiError

try:
slack_client = _login_slack()
response = slack_client.conversations_leave(channel=channel_id)
Expand All @@ -155,6 +159,8 @@ def get_slack_channel_information() -> str:
SlackApiError: If there is an error during get slack channel
information.
"""
from slack_sdk.errors import SlackApiError

try:
slack_client = _login_slack()
response = slack_client.conversations_list()
Expand Down Expand Up @@ -189,6 +195,8 @@ def get_slack_channel_message(channel_id: str) -> str:
Raises:
SlackApiError: If there is an error during get slack channel message.
"""
from slack_sdk.errors import SlackApiError

try:
slack_client = _login_slack()
result = slack_client.conversations_history(channel=channel_id)
Expand Down Expand Up @@ -222,6 +230,8 @@ def send_slack_message(
Raises:
SlackApiError: If an error occurs while sending the message.
"""
from slack_sdk.errors import SlackApiError

try:
slack_client = _login_slack()
if user:
Expand Down Expand Up @@ -254,6 +264,8 @@ def delete_slack_message(
Raises:
SlackApiError: If an error occurs while sending the message.
"""
from slack_sdk.errors import SlackApiError

try:
slack_client = _login_slack()
response = slack_client.chat_delete(channel=channel_id, ts=time_stamp)
Expand Down

0 comments on commit 28621b5

Please sign in to comment.