Skip to content
This repository has been archived by the owner on Aug 13, 2024. It is now read-only.

Add index function to assistant #52

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
4 changes: 4 additions & 0 deletions examples/next/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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')}
Expand Down
Original file line number Diff line number Diff line change
@@ -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)]
27 changes: 26 additions & 1 deletion packages/openassistants/openassistants/core/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 (
Expand Down Expand Up @@ -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(
Expand All @@ -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]:
Expand Down
3 changes: 1 addition & 2 deletions packages/openassistants/openassistants/functions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion packages/openassistants/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "definitive-openassistants"
version = "0.0.4"
version = "0.0.5"
description = ""
authors = ["Rick Lamers <[email protected]>"]
readme = "README.md"
Expand Down