Skip to content
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

fix append_oai_message #47

Merged
merged 8 commits into from
Oct 2, 2023
Merged
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
1 change: 1 addition & 0 deletions .github/workflows/openai.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ jobs:
OAI_CONFIG_LIST: ${{ secrets.OAI_CONFIG_LIST }}
run: |
pip install nbconvert nbformat ipykernel
coverage run -a -m pytest test/test_with_openai.py
coverage run -a -m pytest test/test_notebook.py
coverage xml
cat "$(pwd)/test/executed_openai_notebook_output.txt"
Expand Down
18 changes: 12 additions & 6 deletions autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ def _append_oai_message(self, message: Union[Dict, str], role, conversation_id:
If the message received is a string, it will be put in the "content" field of the new dictionary.
If the message received is a dictionary but does not have any of the two fields "content" or "function_call",
this message is not a valid ChatCompletion message.
If only "function_call" is provided, "content" will be set to None if not provided, and the role of the message will be forced "assistant".

Args:
message (dict or str): message to be appended to the ChatCompletion conversation.
Expand All @@ -271,10 +272,15 @@ def _append_oai_message(self, message: Union[Dict, str], role, conversation_id:
message = self._message_to_dict(message)
# create oai message to be appended to the oai conversation that can be passed to oai directly.
oai_message = {k: message[k] for k in ("content", "function_call", "name", "context") if k in message}
if "content" not in oai_message and "function_call" not in oai_message:
return False
if "content" not in oai_message:
if "function_call" in oai_message:
oai_message["content"] = None # if only function_call is provided, content will be set to None.
else:
return False

oai_message["role"] = "function" if message.get("role") == "function" else role
if "function_call" in oai_message:
oai_message["role"] = "assistant" # only messages with role 'assistant' can have a function call.
self._oai_messages[conversation_id].append(oai_message)
return True

Expand All @@ -289,8 +295,8 @@ def send(

Args:
message (dict or str): message to be sent.
The message could contain the following fields (either content or function_call must be provided):
- content (str): the content of the message.
The message could contain the following fields:
- content (str): Required, the content of the message. (Can be None)
- function_call (str): the name of the function to be called.
- name (str): the name of the function to be called.
- role (str): the role of the message, any role that is not "function"
Expand Down Expand Up @@ -338,8 +344,8 @@ async def a_send(

Args:
message (dict or str): message to be sent.
The message could contain the following fields (either content or function_call must be provided):
- content (str): the content of the message.
The message could contain the following fields:
- content (str): Required, the content of the message. (Can be None)
- function_call (str): the name of the function to be called.
- name (str): the name of the function to be called.
- role (str): the role of the message, any role that is not "function"
Expand Down
61 changes: 61 additions & 0 deletions test/test_with_openai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import autogen
import pytest
import sys

try:
import openai

skip = False
except ImportError:
skip = True


@pytest.mark.skipif(
skip or not sys.version.startswith("3.10"),
reason="do not run if openai is not installed or py!=3.10",
)
def test_function_call_groupchat():
import random

def get_random_number():
return random.randint(0, 100)

config_list_gpt4 = autogen.config_list_from_json(
"OAI_CONFIG_LIST",
filter_dict={
"model": ["gpt-4", "gpt-4-0314", "gpt4", "gpt-4-32k", "gpt-4-32k-0314", "gpt-4-32k-v0314"],
},
)
llm_config = {
"config_list": config_list_gpt4,
"seed": 42,
"functions": [
{
"name": "get_random_number",
"description": "Get a random number between 0 and 100",
"parameters": {
"type": "object",
"properties": {},
},
},
],
}
user_proxy = autogen.UserProxyAgent(
name="User_proxy",
system_message="A human admin that will execute function_calls.",
function_map={"get_random_number": get_random_number},
human_input_mode="NEVER",
)
coder = autogen.AssistantAgent(
name="Player",
system_message="You will can function `get_random_number` to get a random number. Stop only when you get at least 1 even number and 1 odd number. Reply TERMINATE to stop.",
llm_config=llm_config,
)
groupchat = autogen.GroupChat(agents=[user_proxy, coder], messages=[], max_round=7)
manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)

user_proxy.initiate_chat(manager, message="Let's start the game!")


if __name__ == "__main__":
test_function_call_groupchat()
Loading