Skip to content

Commit

Permalink
some refinement needed
Browse files Browse the repository at this point in the history
  • Loading branch information
eavanvalkenburg committed Nov 9, 2023
1 parent 094cb20 commit 993cb70
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
27 changes: 26 additions & 1 deletion python/src/planners/handlebar_planner.prompt.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
name: HandlebarsPlanner
template: |
{{#if last_error}}
{{#message role="user"}}Can you help me write a Handlebars template that achieves the following goal?{{/message}}
{{#message role="user"}}{{goal}}{{/message}}
{{#message role="assistant"}}```
{{last_plan}}
```{{/message}}
{{#message role="user"}}This plan doesn't work, I get this error: "{{last_error}}"{{/message}}
{{#message role="assistant"}}I'm sorry, let me try to fix it.{{/message}}
{{#message role="system"}}## Instructions
Explain how to achieve the user's goal with the available helpers with a Handlebars template.
A previous agent tried to create a Handlebars template, but it was invalid. You must completely rewrite the template so that it no longer has an error by fixing the syntax or by using the correct methods{{/message}}
{{else}}
{{#message role="user"}}Can you help me write a Handlebars template that achieves the following goal?{{/message}}
{{#message role="user"}}{{goal}}{{/message}}
{{#message role="system"}}## Instructions
Create a Handlebars template that describes the steps necessary to accomplish the user's goal in a numbered list.
{{/message}}
{{/if}}
{{#message role="system"}}
## Example
If the user wanted you to generate 10 random numbers and use them in another helper, you could answer with the following.{{/message}}
Expand Down Expand Up @@ -135,6 +152,14 @@ input_variables:
type: string
description: The user's goal that the assistant should accomplish.
is_required: true
- name: last_plan
type: string
description: the previous plan if available
is_required: false
- name: last_error
type: string
description: the previous error if available
is_required: false
output_variable:
type: string
description: The plan that the assistant will use to accomplish the user's goal.
Expand Down
2 changes: 0 additions & 2 deletions python/src/planners/handlebars_planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ async def create_plan(
self, goal: str, variables: dict[str, Any] | None = None
) -> HandleBarsPlan:
# read template
print(os.getcwd())
planner_function = SemanticFunction.from_path(
path=os.getcwd() + "/python/src/planners/handlebar_planner.prompt.yaml"
)
Expand All @@ -48,7 +47,6 @@ async def create_plan(
},
request_settings={"stream": False},
)
# example plan: 'Steps to achieve the goal:\n1. Set a variable named "sum" to the sum of the numbers 3 and 3 (Guess: 6)\n2. Print the value of the "sum" variable\n\nHandlebars Template:\n```\n{{set name="sum" value=(add 3 3)}}\n1. Set a variable named "sum" to the sum of the numbers 3 and 3: {{sum}}.\n```'
return HandleBarsPlan(kernel=self.kernel, template=plan["result"])

def _should_include_function(self, plugin: str, function: str) -> bool:
Expand Down
1 change: 1 addition & 0 deletions python/src/plugins/semantic_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ async def run_async(
variables,
services=None,
request_settings: dict[str, any] | None = None,
*args,
**kwargs,
) -> dict:
if "service" not in kwargs:
Expand Down
2 changes: 1 addition & 1 deletion python/src/plugins/sk_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SKFunction(SKBaseModel):
plugin_name: str = ""

@abstractmethod
async def run_async(self, variables, **kwargs) -> dict:
async def run_async(self, variables, *args, **kwargs) -> dict:
pass

@property
Expand Down
12 changes: 4 additions & 8 deletions python/src/template_engine/handlebars_prompt_template_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,21 @@ def _camel_case(this, *args, **kwargs):

class RunThread(threading.Thread):
# TODO: replace with better solution and/or figure out why asyncio.run will not work, or move to handlebars implementation that van handle async
def __init__(self, func, fixed_kwargs, args, kwargs):
def __init__(self, func, args, kwargs):
self.func = func
self.args = args
self.fixed_kwargs = fixed_kwargs
self.kwargs = kwargs
self.result = None
super().__init__()

def run(self):
kwa = {"variables": self.kwargs}
kwa.update(self.fixed_kwargs)
self.result = asyncio.run(self.func(*self.args, **kwa))
self.result = asyncio.run(self.func(*self.args, **self.kwargs))


def create_func(function, fixed_kwargs):
def func(context, *args, **kwargs):
thread = RunThread(
func=function.run_async, fixed_kwargs=fixed_kwargs, args=args, kwargs=kwargs
)
fixed_kwargs["variables"] = kwargs
thread = RunThread(func=function.run_async, args=args, kwargs=fixed_kwargs)
thread.start()
thread.join()
return thread.result
Expand Down

0 comments on commit 993cb70

Please sign in to comment.