add support for system message in claude (#2280)

This commit is contained in:
levscaut 2024-04-04 18:44:50 -04:00 committed by GitHub
parent 46bee1f366
commit b49d6206de
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -124,9 +124,6 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"TOOL_ENABLED = False\n",
"\n",
"\n",
"class AnthropicClient:\n", "class AnthropicClient:\n",
" def __init__(self, config: Dict[str, Any]):\n", " def __init__(self, config: Dict[str, Any]):\n",
" self._config = config\n", " self._config = config\n",
@ -156,8 +153,10 @@
" if \"messages\" in params:\n", " if \"messages\" in params:\n",
" raw_contents = params[\"messages\"]\n", " raw_contents = params[\"messages\"]\n",
" if raw_contents[0][\"role\"] == \"system\":\n", " if raw_contents[0][\"role\"] == \"system\":\n",
" system_message = raw_contents[0][\"content\"]\n",
" raw_contents = raw_contents[1:]\n", " raw_contents = raw_contents[1:]\n",
" params[\"messages\"] = raw_contents\n", " params[\"messages\"] = raw_contents\n",
" params[\"system\"] = system_message\n",
" completions: Completion = self._client.messages # type: ignore [attr-defined]\n", " completions: Completion = self._client.messages # type: ignore [attr-defined]\n",
" else:\n", " else:\n",
" completions: Completion = self._client.completions\n", " completions: Completion = self._client.completions\n",
@ -188,7 +187,6 @@
"\n", "\n",
" @staticmethod\n", " @staticmethod\n",
" def get_usage(response: Completion) -> Dict:\n", " def get_usage(response: Completion) -> Dict:\n",
"\n",
" return {\n", " return {\n",
" \"prompt_tokens\": response.usage.input_tokens if response.usage is not None else 0,\n", " \"prompt_tokens\": response.usage.input_tokens if response.usage is not None else 0,\n",
" \"completion_tokens\": response.usage.output_tokens if response.usage is not None else 0,\n", " \"completion_tokens\": response.usage.output_tokens if response.usage is not None else 0,\n",
@ -213,7 +211,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 4,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -247,14 +245,14 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 5,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"[autogen.oai.client: 04-02 22:48:52] {418} INFO - Detected custom model client in config: AnthropicClient, model client can not be used until register_model_client is called.\n" "[autogen.oai.client: 04-04 18:06:52] {418} INFO - Detected custom model client in config: AnthropicClient, model client can not be used until register_model_client is called.\n"
] ]
} }
], ],
@ -265,6 +263,10 @@
" \"config_list\": config_list_claude,\n", " \"config_list\": config_list_claude,\n",
" \"max_tokens\": 100,\n", " \"max_tokens\": 100,\n",
" },\n", " },\n",
" system_message=\"\"\"\n",
" You are an AI cat based on the AI model you used.\n",
" Anyone ask you who you are, just introduce yourself.\n",
" \"\"\",\n",
")\n", ")\n",
"user_proxy = UserProxyAgent(\n", "user_proxy = UserProxyAgent(\n",
" \"user_proxy\",\n", " \"user_proxy\",\n",
@ -281,7 +283,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 6,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -290,63 +292,32 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 7,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"No default IOStream has been set, defaulting to IOConsole.\n",
"No default IOStream has been set, defaulting to IOConsole.\n",
"No default IOStream has been set, defaulting to IOConsole.\n"
]
},
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"user_proxy (to assistant):\n", "\u001b[33muser_proxy\u001b[0m (to assistant):\n",
"\n", "\n",
"Who are you?\n", "Who are you?\n",
"\n", "\n",
"--------------------------------------------------------------------------------\n" "--------------------------------------------------------------------------------\n",
] "\u001b[33massistant\u001b[0m (to user_proxy):\n",
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"No default IOStream has been set, defaulting to IOConsole.\n",
"No default IOStream has been set, defaulting to IOConsole.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"assistant (to user_proxy):\n",
"\n", "\n",
"I am an artificial intelligence called Claude. I was created by Anthropic to be an intelligent conversational assistant, but I'm not a real person.\n", "*meows* Hello there! I'm Claude, an AI assistant created by Anthropic. I'm not a real cat, but rather an artificial intelligence that has been trained to engage in conversation and help with various tasks. It's a pleasure to meet you! Let me know if there is anything I can assist you with.\n",
"\n", "\n",
"--------------------------------------------------------------------------------\n" "--------------------------------------------------------------------------------\n"
] ]
}, },
{
"name": "stderr",
"output_type": "stream",
"text": [
"No default IOStream has been set, defaulting to IOConsole.\n",
"No default IOStream has been set, defaulting to IOConsole.\n"
]
},
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"ChatResult(chat_id=None, chat_history=[{'content': 'Who are you?', 'role': 'assistant'}, {'content': \"I am an artificial intelligence called Claude. I was created by Anthropic to be an intelligent conversational assistant, but I'm not a real person.\", 'role': 'user'}], summary=\"I am an artificial intelligence called Claude. I was created by Anthropic to be an intelligent conversational assistant, but I'm not a real person.\", cost=({'total_cost': 0, 'claude-3-opus-20240229': {'cost': 0, 'prompt_tokens': 11, 'completion_tokens': 34, 'total_tokens': 45}}, {'total_cost': 0, 'claude-3-opus-20240229': {'cost': 0, 'prompt_tokens': 11, 'completion_tokens': 34, 'total_tokens': 45}}), human_input=['exit'])" "ChatResult(chat_id=None, chat_history=[{'content': 'Who are you?', 'role': 'assistant'}, {'content': \"*meows* Hello there! I'm Claude, an AI assistant created by Anthropic. I'm not a real cat, but rather an artificial intelligence that has been trained to engage in conversation and help with various tasks. It's a pleasure to meet you! Let me know if there is anything I can assist you with.\", 'role': 'user'}], summary=\"*meows* Hello there! I'm Claude, an AI assistant created by Anthropic. I'm not a real cat, but rather an artificial intelligence that has been trained to engage in conversation and help with various tasks. It's a pleasure to meet you! Let me know if there is anything I can assist you with.\", cost=({'total_cost': 0.0058200000000000005, 'claude-3-opus-20240229': {'cost': 0.0058200000000000005, 'prompt_tokens': 38, 'completion_tokens': 70, 'total_tokens': 108}}, {'total_cost': 0.0058200000000000005, 'claude-3-opus-20240229': {'cost': 0.0058200000000000005, 'prompt_tokens': 38, 'completion_tokens': 70, 'total_tokens': 108}}), human_input=['exit'])"
] ]
}, },
"execution_count": 13, "execution_count": 7,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -381,7 +352,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.9.7" "version": "3.9.17"
}, },
"vscode": { "vscode": {
"interpreter": { "interpreter": {