Skip to content

Commit

Permalink
fix: improve handling of None values in format_text_item and update t…
Browse files Browse the repository at this point in the history
…ool assignment logic in BranchOperationMixin
  • Loading branch information
ohdearquant committed Dec 25, 2024
1 parent e088e92 commit 3b3c9a3
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 46 deletions.
6 changes: 4 additions & 2 deletions lionagi/core/communication/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,11 @@ def format_text_item(item: Any) -> str:
for j in item:
if isinstance(j, dict):
for k, v in j.items():
msg += f"- {k}: {v} \n\n"
if v is not None:
msg += f"- {k}: {v} \n\n"
else:
msg += f"{j}\n"
if j is not None:
msg += f"{j}\n"
return msg


Expand Down
2 changes: 2 additions & 0 deletions lionagi/core/session/branch_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ async def operate(
if auto_retry_parse is True:
operative.auto_retry_parse = True

if actions:
tools = tools or True
if invoke_actions and tools:
tool_schemas = self.get_tool_schema(tools)

Expand Down
147 changes: 103 additions & 44 deletions notebooks/test_action.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
" this is a test function it will return the query as it is\n",
"\n",
" Args:\n",
" query: str: query to be returned\n",
" query: str: query to be returned\n",
"\n",
" Returns:\n",
" str: query as it is\n",
" str: query as it is\n",
" \"\"\"\n",
" return query\n",
"\n",
Expand All @@ -62,7 +62,7 @@
"outputs": [],
"source": [
"result = await french.operate(\n",
" instruction=\"hello? you should test the test_func with a sample query\",\n",
" instruction=\"hello? you should test the test_func with a sample queries\",\n",
" operative_model=Test,\n",
" actions=True,\n",
")"
Expand All @@ -76,7 +76,7 @@
{
"data": {
"text/plain": [
"Test(comments='Testing test_func with a sample query')"
"Test(comments='Testing the test_func with a sample query')"
]
},
"execution_count": 4,
Expand Down Expand Up @@ -147,48 +147,48 @@
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2024-12-25 04:36:43.576884+00:00</td>\n",
" <td>2024-12-25 05:06:07.973422+00:00</td>\n",
" <td>Instruction</td>\n",
" <td>user</td>\n",
" <td>{'context': [{'respond_schema_info': {'$defs':...</td>\n",
" <td>bb234dfb-ae49-47c1-9194-eed2a79ac9cd</td>\n",
" <td>8a65911c-a909-49b0-8bc8-10baf041751a</td>\n",
" <td>user</td>\n",
" <td>bb8c6aa5-2ccf-4fc1-ac39-b1b88b11436d</td>\n",
" <td>{'last_updated': {'context': 1735101403.970189}}</td>\n",
" <td>14132072-3812-4b58-ac53-065b37a19b75</td>\n",
" <td>{'last_updated': {'context': 1735103168.417341}}</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2024-12-25 04:36:43.970026+00:00</td>\n",
" <td>2024-12-25 05:06:08.417155+00:00</td>\n",
" <td>AssistantResponse</td>\n",
" <td>assistant</td>\n",
" <td>{'assistant_response': '```json\n",
"{\n",
" \"comments\"...</td>\n",
" <td>e58487a7-ef69-4d5a-8a7d-703b852f0d8c</td>\n",
" <td>bb8c6aa5-2ccf-4fc1-ac39-b1b88b11436d</td>\n",
" <td>ddb5279b-81d3-498b-ae9e-bb45eedbce8d</td>\n",
" <td>14132072-3812-4b58-ac53-065b37a19b75</td>\n",
" <td>N/A</td>\n",
" <td>{'model_response': {'choices': [{'index': 0, '...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2024-12-25 04:36:43.972039+00:00</td>\n",
" <td>2024-12-25 05:06:08.418792+00:00</td>\n",
" <td>ActionRequest</td>\n",
" <td>assistant</td>\n",
" <td>{'action_request': {'function': 'test_func', '...</td>\n",
" <td>fc9f7361-b1ea-4441-8679-1e8912ea2afe</td>\n",
" <td>bb8c6aa5-2ccf-4fc1-ac39-b1b88b11436d</td>\n",
" <td>35db0be2-6d2a-42e3-aa7f-78785ddf46ec</td>\n",
" <td>9f2601af-88f2-4f69-8f69-44753469d3c0</td>\n",
" <td>14132072-3812-4b58-ac53-065b37a19b75</td>\n",
" <td>0f7df12b-e07c-4c01-a52f-9a892ce0564d</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2024-12-25 04:36:43.972235+00:00</td>\n",
" <td>2024-12-25 05:06:08.418938+00:00</td>\n",
" <td>ActionResponse</td>\n",
" <td>assistant</td>\n",
" <td>{'action_request_id': fc9f7361-b1ea-4441-8679-...</td>\n",
" <td>550f73f8-f5fc-489b-a69a-59f65c1942f8</td>\n",
" <td>35db0be2-6d2a-42e3-aa7f-78785ddf46ec</td>\n",
" <td>bb8c6aa5-2ccf-4fc1-ac39-b1b88b11436d</td>\n",
" <td>{'action_request_id': 9f2601af-88f2-4f69-8f69-...</td>\n",
" <td>cff04174-d7df-4c8d-a66a-8a3fc877ba2b</td>\n",
" <td>0f7df12b-e07c-4c01-a52f-9a892ce0564d</td>\n",
" <td>14132072-3812-4b58-ac53-065b37a19b75</td>\n",
" <td>{}</td>\n",
" </tr>\n",
" </tbody>\n",
Expand All @@ -197,33 +197,33 @@
],
"text/plain": [
" timestamp lion_class role \\\n",
"0 2024-12-25 04:36:43.576884+00:00 Instruction user \n",
"1 2024-12-25 04:36:43.970026+00:00 AssistantResponse assistant \n",
"2 2024-12-25 04:36:43.972039+00:00 ActionRequest assistant \n",
"3 2024-12-25 04:36:43.972235+00:00 ActionResponse assistant \n",
"0 2024-12-25 05:06:07.973422+00:00 Instruction user \n",
"1 2024-12-25 05:06:08.417155+00:00 AssistantResponse assistant \n",
"2 2024-12-25 05:06:08.418792+00:00 ActionRequest assistant \n",
"3 2024-12-25 05:06:08.418938+00:00 ActionResponse assistant \n",
"\n",
" content \\\n",
"0 {'context': [{'respond_schema_info': {'$defs':... \n",
"1 {'assistant_response': '```json\n",
"{\n",
" \"comments\"... \n",
"2 {'action_request': {'function': 'test_func', '... \n",
"3 {'action_request_id': fc9f7361-b1ea-4441-8679-... \n",
"3 {'action_request_id': 9f2601af-88f2-4f69-8f69-... \n",
"\n",
" ln_id sender \\\n",
"0 bb234dfb-ae49-47c1-9194-eed2a79ac9cd user \n",
"1 e58487a7-ef69-4d5a-8a7d-703b852f0d8c bb8c6aa5-2ccf-4fc1-ac39-b1b88b11436d \n",
"2 fc9f7361-b1ea-4441-8679-1e8912ea2afe bb8c6aa5-2ccf-4fc1-ac39-b1b88b11436d \n",
"3 550f73f8-f5fc-489b-a69a-59f65c1942f8 35db0be2-6d2a-42e3-aa7f-78785ddf46ec \n",
"0 8a65911c-a909-49b0-8bc8-10baf041751a user \n",
"1 ddb5279b-81d3-498b-ae9e-bb45eedbce8d 14132072-3812-4b58-ac53-065b37a19b75 \n",
"2 9f2601af-88f2-4f69-8f69-44753469d3c0 14132072-3812-4b58-ac53-065b37a19b75 \n",
"3 cff04174-d7df-4c8d-a66a-8a3fc877ba2b 0f7df12b-e07c-4c01-a52f-9a892ce0564d \n",
"\n",
" recipient \\\n",
"0 bb8c6aa5-2ccf-4fc1-ac39-b1b88b11436d \n",
"0 14132072-3812-4b58-ac53-065b37a19b75 \n",
"1 N/A \n",
"2 35db0be2-6d2a-42e3-aa7f-78785ddf46ec \n",
"3 bb8c6aa5-2ccf-4fc1-ac39-b1b88b11436d \n",
"2 0f7df12b-e07c-4c01-a52f-9a892ce0564d \n",
"3 14132072-3812-4b58-ac53-065b37a19b75 \n",
"\n",
" metadata \n",
"0 {'last_updated': {'context': 1735101403.970189}} \n",
"0 {'last_updated': {'context': 1735103168.417341}} \n",
"1 {'model_response': {'choices': [{'index': 0, '... \n",
"2 {} \n",
"3 {} "
Expand All @@ -248,17 +248,17 @@
"text/plain": [
"{'choices': [{'index': 0,\n",
" 'message': {'role': 'assistant',\n",
" 'content': '```json\\n{\\n \"comments\": \"Testing test_func with a sample query\",\\n \"action_requests\": [\\n {\\n \"function\": \"test_func\",\\n \"arguments\": {\\n \"query\": \"sample query\"\\n }\\n }\\n ],\\n \"action_required\": true\\n}\\n```'},\n",
" 'content': '```json\\n{\\n \"comments\": \"Testing the test_func with a sample query\",\\n \"action_requests\": [\\n {\\n \"function\": \"test_func\",\\n \"arguments\": {\\n \"query\": \"This is a sample query\"\\n }\\n }\\n ],\\n \"action_required\": true\\n}\\n```'},\n",
" 'logprobs': None,\n",
" 'finish_reason': 'stop'}],\n",
" 'model': 'llama-3.3-70b-versatile',\n",
" 'usage': {'queue_time': 0.0006969120000000009,\n",
" 'prompt_tokens': 522,\n",
" 'prompt_time': 0.055661427,\n",
" 'completion_tokens': 62,\n",
" 'completion_time': 0.225454545,\n",
" 'total_tokens': 584,\n",
" 'total_time': 0.281115972}}"
" 'usage': {'queue_time': 0.000735520999999989,\n",
" 'prompt_tokens': 608,\n",
" 'prompt_time': 0.064599073,\n",
" 'completion_tokens': 66,\n",
" 'completion_time': 0.24,\n",
" 'total_tokens': 674,\n",
" 'total_time': 0.304599073}}"
]
},
"execution_count": 7,
Expand All @@ -274,6 +274,61 @@
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"a = french.msgs.instructions[0]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Instruction(ln_id=IDType(8a65911c-a909-49b0-8bc8-10baf041751a), created_timestamp=2024-12-25 05:06:07.973422, content={'context': [{'respond_schema_info': {'$defs': {'ActionRequestModel': {'properties': {'function':..., metadata={'last_updated': {'context': 1735103168.417341}}, created_timestamp=1735103167.973422, sender=user, recipient=14132072-3812-4b58-ac53-065b37a19b75, role=user, extra_fields={})"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'tools': [{'type': 'function',\n",
" 'function': {'name': 'test_func',\n",
" 'description': 'this is a test function it will return the query as it is',\n",
" 'parameters': {'type': 'object',\n",
" 'properties': {'query': {'type': 'string',\n",
" 'description': 'str: query to be returned'}},\n",
" 'required': ['query']}}}]}"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"french.msgs.instructions[0].tool_schemas"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
Expand All @@ -284,11 +339,15 @@
"## **Task context**\n",
"- respond_schema_info: {'$defs': {'ActionRequestModel': {'properties': {'function': {'anyOf': [{'type': 'string'}, {'type': 'null'}], 'default': None, 'description': \"Name of the function to call from the provided `tool_schemas`. If no `tool_schemas` exist, set to None or leave blank. Never invent new function names outside what's given.\", 'examples': ['add', 'multiply', 'divide'], 'title': 'Function'}, 'arguments': {'anyOf': [{'type': 'object'}, {'type': 'null'}], 'description': 'Dictionary of arguments for the chosen function. Use only argument names/types defined in `tool_schemas`. Never introduce extra argument names.', 'examples': [{'num1': 1, 'num2': 2}, {'x': 'hello', 'y': 'world'}], 'title': 'Action Arguments'}}, 'title': 'ActionRequestModel', 'type': 'object'}}, 'properties': {'comments': {'title': 'Comments', 'type': 'string'}, 'action_requests': {'description': 'List of actions to be executed when `action_required` is true. Each action must align with the available `tool_schemas`. Leave empty if no actions are needed.', 'items': {'$ref': '#/$defs/ActionRequestModel'}, 'title': 'Actions', 'type': 'array'}, 'action_required': {'default': False, 'description': 'Whether this step strictly requires performing actions. If true, the requests in `action_requests` must be fulfilled, assuming `tool_schemas` are available. If false or no `tool_schemas` exist, actions are optional.', 'title': 'Action Required', 'type': 'boolean'}}, 'required': ['comments'], 'title': 'Test', 'type': 'object'} \n",
"\n",
"None\n",
"\n",
"\n",
"## **Task instruction**\n",
"hello? you should test the test_func with a sample query\n",
"hello? you should test the test_func with a sample queries\n",
"\n",
"\n",
"## **Task tool_schemas**\n",
"- tools: [{'type': 'function', 'function': {'name': 'test_func', 'description': 'this is a test function it will return the query as it is', 'parameters': {'type': 'object', 'properties': {'query': {'type': 'string', 'description': 'str: query to be returned'}}, 'required': ['query']}}}] \n",
"\n",
"\n",
"\n",
"## **Task response format**\n",
Expand All @@ -306,7 +365,7 @@
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 8,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
Expand Down

0 comments on commit 3b3c9a3

Please sign in to comment.