Skip to content

Commit

Permalink
Merge pull request #54 from SylphAI-Inc/main
Browse files Browse the repository at this point in the history
merge
  • Loading branch information
liyin2015 authored Jun 29, 2024
2 parents 5242d8e + 65d0bdf commit 6d53843
Show file tree
Hide file tree
Showing 14 changed files with 1,941 additions and 822 deletions.
Empty file added developer_notes/agent.py
Empty file.
492 changes: 492 additions & 0 deletions developer_notes/dataclass.ipynb

Large diffs are not rendered by default.

57 changes: 35 additions & 22 deletions developer_notes/tools.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from dataclasses import dataclass
from dataclasses import dataclass, field
from typing import List
import numpy as np
import time
import asyncio
from lightrag.utils import setup_env # noqa
from lightrag.core import Component
from lightrag.core import Component, DataClass
from lightrag.core.types import Function, FunctionExpression
from lightrag.core.tool_manager import ToolManager
from lightrag.components.output_parsers import JsonOutputParser
Expand Down Expand Up @@ -55,6 +55,14 @@ def add_points(p1: Point, p2: Point) -> Point:
return Point(p1.x + p2.x, p1.y + p2.y)


@dataclass # TODO: data class schema, need to go all the way down to the subclass
class MultipleFunctionDefinition(DataClass):
a: Point = field(metadata={"desc": "First number"})
b: int = field(metadata={"desc": "Second number"})


# optionally to define schma yourself, this can be used to generate FunctionDefinition
print(MultipleFunctionDefinition.to_schema_str())
# use function tool

template = r"""<SYS>You have these tools available:
Expand Down Expand Up @@ -110,8 +118,7 @@ def add_points(p1: Point, p2: Point) -> Point:
<OUTPUT_FORMAT>
Here is how you call one function.
{{output_format_str}}
Awlays return a List using `[]` of the above JSON objects. You can have length of 1 or more.
Do not call multiple functions in one action field.
-Always return a List using `[]` of the above JSON objects, even if its just one item.
</OUTPUT_FORMAT>
<SYS>
{{input_str}}
Expand All @@ -136,16 +143,14 @@ def __init__(self):

def prepare_single_function_call_generator(self):
tool_manager = ToolManager(tools=functions)
func_parser = JsonOutputParser(data_class=Function)
instructions = func_parser.format_instructions(exclude=["thought"])
print(instructions)
func_parser = JsonOutputParser(
data_class=Function, exclude_fields=["thought", "args"]
)

model_kwargs = {"model": "gpt-3.5-turbo"}
prompt_kwargs = {
"tools": tool_manager.yaml_definitions,
"output_format_str": func_parser.format_instructions(
exclude=["thought", "args"]
),
"output_format_str": func_parser.format_instructions(),
}
generator = Generator(
model_client=ModelClientType.OPENAI(),
Expand Down Expand Up @@ -191,16 +196,16 @@ def prepare_single_function_call_generator(self):
"Point": Point,
},
)
func_parser = JsonOutputParser(data_class=FunctionExpression)
instructions = func_parser.format_instructions(exclude=["thought"])
func_parser = JsonOutputParser(
data_class=FunctionExpression, exclude_fields=["thought", "args"]
)
instructions = func_parser.format_instructions()
print(instructions)

model_kwargs = {"model": "gpt-4o"}
prompt_kwargs = {
"tools": tool_manager.yaml_definitions,
"output_format_str": func_parser.format_instructions(
exclude=["thought", "args"]
),
"output_format_str": func_parser.format_instructions(),
"context_str": tool_manager._additional_context,
}
generator = Generator(
Expand Down Expand Up @@ -287,16 +292,21 @@ def prepare_single_function_call_generator(self):
"Point": Point,
},
)
func_parser = JsonOutputParser(data_class=FunctionExpression)
instructions = func_parser.format_instructions(exclude=["thought"])
example = FunctionExpression.from_function(
func=add_points, p1=Point(x=1, y=2), p2=Point(x=3, y=4)
)
func_parser = JsonOutputParser(
data_class=FunctionExpression,
examples=[example],
exclude_fields=["thought"],
)
instructions = func_parser.format_instructions()
print(instructions)

model_kwargs = {"model": "gpt-4o"}
prompt_kwargs = {
"tools": tool_manager.yaml_definitions,
"output_format_str": func_parser.format_instructions(
exclude=["thought", "args"]
),
"output_format_str": func_parser.format_instructions(),
}
generator = Generator(
model_client=ModelClientType.OPENAI(),
Expand All @@ -317,13 +327,13 @@ def run_function_call(self, generator: Generator, tool_manager: ToolManager):
print(f"{'-'*50}")
try:
result = generator(prompt_kwargs=prompt_kwargs)
# print(f"LLM raw output: {result.raw_response}")
print(f"LLM raw output: {result.raw_response}")
func_expr: List[FunctionExpression] = [
FunctionExpression.from_dict(item) for item in result.data
]
print(f"Function_expr: {func_expr}")
for expr in func_expr:
func_output = tool_manager.execute_func_expr_via_sandbox(expr)
func_output = tool_manager.execute_func_expr_via_eval(expr)
print(f"Function output: {func_output}")
except Exception as e:
print(
Expand All @@ -343,6 +353,9 @@ def run_function_call(self, generator: Generator, tool_manager: ToolManager):
# fc.run_function_call(generator, tool_manager) # 15.92s
# asyncio.run(fc.run_async_function_call(generator, tool_manager)) # 7.8s

output = eval("add(a=y, b=5)", {"y": 3, "add": add})
print(output)

mul_fc = MultiFunctionCallWithFunctionExpression()
generator, tool_manager = mul_fc.prepare_single_function_call_generator()
mul_fc.run_function_call(generator, tool_manager)
Loading

0 comments on commit 6d53843

Please sign in to comment.