Skip to content

Commit

Permalink
v0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ju-bezdek committed Jan 6, 2024
1 parent 6dcc342 commit 5fe718f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,7 @@ New parameters in llm decorator
- minor fixes

## Version 0.4.2 (2023-12-20)
- critical bugfix - Assistant messages without context (text) but only with arguments were ignored
- critical bugfix - Assistant messages without context (text) but only with arguments were ignored

## Version 0.5.0 (2024-01-06)
- ability to pass in function to augment function arguments before executing in OutputWithFunctionCall
2 changes: 1 addition & 1 deletion src/langchain_decorators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
from .function_decorator import llm_function, get_function_schema
from .chains import FunctionsProvider, FollowupHandle

__version__="0.4.2"
__version__="0.5.0"


2 changes: 1 addition & 1 deletion src/langchain_decorators/output_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __str__(self) -> str:


class ListOutputParser(BaseOutputParser):
"""Class to parse the output of an LLM call to a list."""
"""Class to parse the output of an LLM call in a bullet/numbered list format to a list."""

@property
def _type(self) -> str:
Expand Down
10 changes: 6 additions & 4 deletions src/langchain_decorators/prompt_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ def partial_formatter(inputs, _partial=partial, _partial_input_variables=partial
return _partial

partial_builders[partial_name] = partial_formatter

input_variables = [v for _, v, _, _ in Formatter().parse(template) if v is not None and v not in partials_with_params]
try:
input_variables = [v for _, v, _, _ in Formatter().parse(template) if v is not None and v not in partials_with_params]
except ValueError as e:
raise ValueError(f"{e}\nError parsing template: \n```\n{template}\n```")
for partial_name, (partial, partial_input_variables) in partials_with_params.items():
input_variables.extend(partial_input_variables)

Expand Down Expand Up @@ -360,8 +362,8 @@ def get_final_template(self, **kwargs: Any)->PromptTemplate:

for message_draft in self.prompt_template_drafts:
msg_template_final_str = message_draft.finalize_template(kwargs)

parts.append((msg_template_final_str,message_draft.role))
if msg_template_final_str: # skip empty messages / templates
parts.append((msg_template_final_str,message_draft.role))

else:
msg_template_final_str = self.prompt_template_drafts.finalize_template(kwargs)
Expand Down
19 changes: 13 additions & 6 deletions src/langchain_decorators/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,36 @@ def support_async(self):
def support_sync(self):
return bool(self.function)

async def execute_async(self):
async def execute_async(self, augment_args:Callable[[Dict[str,Any]],Dict[str,Any]]=None):
"""Executes the function asynchronously."""
if not (self.function or self.function_async):
raise ValueError("No function to execute")
call_args = self.function_arguments or {}
if augment_args:
call_args = augment_args(call_args)
if self.function_async:
result= await self.function_async(**(self.function_arguments or {}))
result= await self.function_async(**call_args)
else:
result= self.function(**(self.function_arguments or {}))
result= self.function(**call_args)
if result and asyncio.iscoroutine(result):
# this handles special scenario when fake @llm_function is used
result = await result
self.result = result
self._result_generated=True
return result

def execute(self):
def execute(self, augment_args:Callable[[Dict[str,Any]],Dict[str,Any]]=None):
""" Executes the function synchronously.
If the function is async, it will be executed in a event loop.
"""
if not (self.function or self.function_async):
raise ValueError("No function to execute")
call_args = self.function_arguments or {}
if augment_args:
call_args = augment_args(**call_args)

if self.function:
result= self.function(**(self.function_arguments or {}))
result= self.function(**call_args)
else:
try:
current_loop = asyncio.get_running_loop()
Expand Down Expand Up @@ -111,7 +118,7 @@ def function_output_to_message(self, function_output=None):
if isinstance(function_output,BaseModel):
function_output = function_output.json()
elif not isinstance(function_output,str):
function_output = json.dumps(function_output)
function_output = repr(function_output)

return FunctionMessage(name=self.function_name, content=function_output)

Expand Down

0 comments on commit 5fe718f

Please sign in to comment.