From c2d42b913a0b6935b196a51101059b4943b69f4b Mon Sep 17 00:00:00 2001 From: Rick Lamers Date: Sun, 17 Dec 2023 20:23:13 +0100 Subject: [PATCH 1/2] Add index function to assistant --- .../piedpiper/search_web_about_hooli.yaml | 2 +- .../send_purchase_inquiry_email.yaml | 2 +- examples/next/src/app/page.tsx | 4 +++ .../openassistants/contrib/index_function.py | 24 +++++++++++++++++ .../openassistants/core/assistant.py | 27 ++++++++++++++++++- .../openassistants/functions/base.py | 3 +-- 6 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 packages/openassistants/openassistants/contrib/index_function.py diff --git a/examples/fast-api-server/library/piedpiper/search_web_about_hooli.yaml b/examples/fast-api-server/library/piedpiper/search_web_about_hooli.yaml index 798831a..027d732 100644 --- a/examples/fast-api-server/library/piedpiper/search_web_about_hooli.yaml +++ b/examples/fast-api-server/library/piedpiper/search_web_about_hooli.yaml @@ -1,6 +1,6 @@ display_name: Search the web using DuckDuckGo description: | - get information from DuckDuckGo + Get information from DuckDuckGo sample_questions: - Search the web for "pied piper" parameters: diff --git a/examples/fast-api-server/library/piedpiper/send_purchase_inquiry_email.yaml b/examples/fast-api-server/library/piedpiper/send_purchase_inquiry_email.yaml index c67048a..71ffa0a 100644 --- a/examples/fast-api-server/library/piedpiper/send_purchase_inquiry_email.yaml +++ b/examples/fast-api-server/library/piedpiper/send_purchase_inquiry_email.yaml @@ -1,6 +1,6 @@ display_name: Send purchase inquiry email description: | - send an inquiry email about a recent purchase to an employee + Send an inquiry email about a recent purchase to an employee sample_questions: - send purchase inquiry email parameters: diff --git a/examples/next/src/app/page.tsx b/examples/next/src/app/page.tsx index d8c7e14..66971d8 100644 --- a/examples/next/src/app/page.tsx +++ b/examples/next/src/app/page.tsx @@ -19,6 +19,10 @@ export default function Home() { title: 'Show spend per product', prompt: 'Show spend per product', }, + { + title: 'What can I ask?', + prompt: 'What can I ask?', + }, ]} welcomeMessage="This is a simple starter for deploying a nextjs app with an open assistant chat widget. This is configured to use the example library which builds off a couple csv of employee and purchase data" getToken={() => Promise.resolve('token')} diff --git a/packages/openassistants/openassistants/contrib/index_function.py b/packages/openassistants/openassistants/contrib/index_function.py new file mode 100644 index 0000000..3e5878f --- /dev/null +++ b/packages/openassistants/openassistants/contrib/index_function.py @@ -0,0 +1,24 @@ +from typing import Awaitable, Callable, List, Literal, Sequence + +from openassistants.data_models.function_output import FunctionOutput, TextOutput +from openassistants.functions.base import ( + BaseFunction, + FunctionExecutionDependency, +) +from openassistants.functions.utils import AsyncStreamVersion + + +class IndexFunction(BaseFunction): + type: Literal["IndexFunction"] = "IndexFunction" + functions: Callable[[], Awaitable[List[BaseFunction]]] + + async def execute( + self, deps: FunctionExecutionDependency + ) -> AsyncStreamVersion[Sequence[FunctionOutput]]: + output = "" + for function in await self.functions(): + if function.type == "IndexFunction": + continue + output += f"""**{function.display_name}** +{function.description}\n\n""" + yield [TextOutput(text=output)] diff --git a/packages/openassistants/openassistants/core/assistant.py b/packages/openassistants/openassistants/core/assistant.py index 5105c97..95813fa 100644 --- a/packages/openassistants/openassistants/core/assistant.py +++ b/packages/openassistants/openassistants/core/assistant.py @@ -5,6 +5,7 @@ from langchain.chat_models.openai import ChatOpenAI from langchain.embeddings import OpenAIEmbeddings from langchain.embeddings.base import Embeddings +from openassistants.contrib.index_function import IndexFunction from openassistants.data_models.chat_messages import ( OpasAssistantMessage, OpasFunctionMessage, @@ -17,7 +18,7 @@ Entity, FunctionExecutionDependency, ) -from openassistants.functions.crud import FunctionCRUD, LocalCRUD +from openassistants.functions.crud import FunctionCRUD, LocalCRUD, PythonCRUD from openassistants.llm_function_calling.entity_resolution import resolve_entities from openassistants.llm_function_calling.fallback import perform_general_qa from openassistants.llm_function_calling.infilling import ( @@ -49,6 +50,7 @@ def __init__( function_fallback: Optional[BaseChatModel] = None, entity_embedding_model: Optional[Embeddings] = None, scope_description: str = "General assistant.", + add_index: bool = True, ): # instantiate dynamically vs as default args self.function_identification = function_identification or ChatOpenAI( @@ -72,6 +74,29 @@ def __init__( library if isinstance(library, FunctionCRUD) else LocalCRUD(library) for library in libraries ] + + if add_index: + self.function_libraries.append( + PythonCRUD( + functions=[ + IndexFunction( + id="index", + display_name="List functions", + description=( + "List the functions available to the assistant. " + "This is a list of things you can ask." + ), + sample_questions=[ + "What can you do?", + "What can I ask?", + "Which functions are defined?", + ], + functions=self.get_all_functions, + ) + ] + ) + ) + self._cached_all_functions = [] async def get_all_functions(self) -> List[BaseFunction]: diff --git a/packages/openassistants/openassistants/functions/base.py b/packages/openassistants/openassistants/functions/base.py index 4f28320..c25a870 100644 --- a/packages/openassistants/openassistants/functions/base.py +++ b/packages/openassistants/openassistants/functions/base.py @@ -44,12 +44,11 @@ async def execute( """ yield [] - @abc.abstractmethod async def get_parameters_json_schema(self) -> dict: """ Get the json schema of the function's parameters """ - pass + return {"type": "object", "properties": {}, "required": []} async def get_signature(self) -> str: json_schema = await self.get_parameters_json_schema() From c646180bb0dd948948c02823dbca2478028ecdd8 Mon Sep 17 00:00:00 2001 From: Rick Lamers Date: Mon, 18 Dec 2023 13:40:32 +0100 Subject: [PATCH 2/2] Bump Opas version. --- packages/openassistants/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/openassistants/pyproject.toml b/packages/openassistants/pyproject.toml index d5bb3e2..9a6e36e 100644 --- a/packages/openassistants/pyproject.toml +++ b/packages/openassistants/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "definitive-openassistants" -version = "0.0.4" +version = "0.0.5" description = "" authors = ["Rick Lamers "] readme = "README.md"