From 18431faa49aefe80a0c41a309cff172b65e39716 Mon Sep 17 00:00:00 2001 From: yashbonde Date: Mon, 16 Oct 2023 16:49:28 +0530 Subject: [PATCH] [chore] automate generation of components list --- .github/workflows/static.yml | 2 + .../cf_server/chainfury_server.api.fury.rst | 7 -- api_docs/cf_server/chainfury_server.api.rst | 1 - api_docs/examples/components-list.rst | 75 +++++++++++++++++++ api_docs/index.rst | 2 +- api_docs/templates/components-list.rst | 54 +++++++++++++ cf_internal | 2 +- chainfury/agent.py | 2 +- chainfury/base.py | 2 +- chainfury/components/qdrant/__init__.py | 2 + scripts/list_builtins.py | 58 ++++++++++++++ 11 files changed, 195 insertions(+), 12 deletions(-) delete mode 100644 api_docs/cf_server/chainfury_server.api.fury.rst create mode 100644 api_docs/examples/components-list.rst create mode 100644 api_docs/templates/components-list.rst create mode 100644 scripts/list_builtins.py diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index e1387f8a..63041b07 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -46,6 +46,8 @@ jobs: python -m pip install sphinx-rtd-theme==1.2.2 cd server python3 -m pip install . + cd .. + python3 scripts/list_builtins.py ./api_docs/templates/components-list.rst ./api_docs/examples/components-list.rst - name: Build docs run: | diff --git a/api_docs/cf_server/chainfury_server.api.fury.rst b/api_docs/cf_server/chainfury_server.api.fury.rst deleted file mode 100644 index 102e71cc..00000000 --- a/api_docs/cf_server/chainfury_server.api.fury.rst +++ /dev/null @@ -1,7 +0,0 @@ -chainfury\_server.api.fury module -================================= - -.. automodule:: chainfury_server.api.fury - :members: - :undoc-members: - :show-inheritance: diff --git a/api_docs/cf_server/chainfury_server.api.rst b/api_docs/cf_server/chainfury_server.api.rst index a1b7c545..5673b0d8 100644 --- a/api_docs/cf_server/chainfury_server.api.rst +++ b/api_docs/cf_server/chainfury_server.api.rst @@ -13,6 +13,5 @@ Submodules :maxdepth: 4 chainfury_server.api.chains - chainfury_server.api.fury chainfury_server.api.prompts chainfury_server.api.user diff --git a/api_docs/examples/components-list.rst b/api_docs/examples/components-list.rst new file mode 100644 index 00000000..838ce6e3 --- /dev/null +++ b/api_docs/examples/components-list.rst @@ -0,0 +1,75 @@ +Components List +=============== + +.. this is a jinja template document, run scripts/list_builtins.py to generate components-list.rst + +There are several components that are shipped with the ``chainfury``. You can find how to access the underlying function +via the `components page`_. + +.. code-block::python + + # load the registries you can do these imports + from chainfury import programatic_actions_registry, ai_actions_registry + +Programatic Actions +------------------- + +Programatic means that these are generally not an LLM call rather something more standard like calling an API, +transforming the data, etc. + + +* `serper-api` - Search the web with Serper. Copy: ``programatic_actions_registry.get("serper-api")`` + +* `call_api_requests` - Call an API using the requests library. Copy: ``programatic_actions_registry.get("call_api_requests")`` + +* `regex_search` - Perform a regex search on the text and get items in an array. Copy: ``programatic_actions_registry.get("regex_search")`` + +* `regex_substitute` - Perform a regex substitution on the text and get the result. Copy: ``programatic_actions_registry.get("regex_substitute")`` + +* `json_translator` - Extract a value from a JSON object using a list of keys. Copy: ``programatic_actions_registry.get("json_translator")`` + + +AI Action Components +-------------------- + +These actions generally take the input, create a custom prompt, call the Model and respond back with the result. + + +* `hello-world` - Python function loaded from a file used as an AI action. Copy: ``ai_actions_registry.get("hello-world")`` + +* `deep-rap-quote` - J-type action will write a deep poem in the style of a character. Copy: ``ai_actions_registry.get("deep-rap-quote")`` + + +Memory Components +----------------- + +Memory components are used to store data, which can be a Vector DB or Redis, etc. + + +* `qdrant-write` - Write to the Qdrant DB using the Qdrant client. Copy: ``memory_registry.get_write("qdrant-write")`` + +* `qdrant-read` - Function to read from the Qdrant DB using the Qdrant client. Copy: ``memory_registry.get_read("qdrant-read")`` + + +Model Components +---------------- + +Model are the different GenAI models that can be used from the ``chainfury``. + + +* `stability-text-to-image` - Generate a new image from a text prompt. Copy: ``model_registry.get("stability-text-to-image")`` + +* `chatnbx` - Chat with the ChatNBX API with OpenAI compatability, see more at https://chat.nbox.ai/. Copy: ``model_registry.get("chatnbx")`` + +* `nbx-deploy` - Call NimbleBox LLMOps deploy API. Copy: ``model_registry.get("nbx-deploy")`` + +* `openai-completion` - Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position. Copy: ``model_registry.get("openai-completion")`` + +* `openai-chat` - Given a list of messages describing a conversation, the model will return a response. Copy: ``model_registry.get("openai-chat")`` + +* `openai-embedding` - Given a list of messages create embeddings for each message. Copy: ``model_registry.get("openai-embedding")`` + + +.. all the links are here + +.. _components page: https://qdrant.tech/documentation/tutorials/bulk-upload/#upload-directly-to-disk diff --git a/api_docs/index.rst b/api_docs/index.rst index aa9f4566..33a9b758 100644 --- a/api_docs/index.rst +++ b/api_docs/index.rst @@ -48,7 +48,6 @@ Read the latest blog posts: examples/install examples/storing-private-data - examples/use-chainfury-privately .. toctree:: :maxdepth: 2 @@ -65,6 +64,7 @@ Read the latest blog posts: :caption: Integrations source/chainfury.components + examples/components-list .. toctree:: :maxdepth: 2 diff --git a/api_docs/templates/components-list.rst b/api_docs/templates/components-list.rst new file mode 100644 index 00000000..02f58eb9 --- /dev/null +++ b/api_docs/templates/components-list.rst @@ -0,0 +1,54 @@ +Components List +=============== + +.. this is a jinja template document, run scripts/list_builtins.py to generate components-list.rst + +There are several components that are shipped with the ``chainfury``. You can find how to access the underlying function +via the `components page`_. + +.. code-block::python + + # load the registries you can do these imports + from chainfury import programatic_actions_registry, ai_actions_registry + +Programatic Actions +------------------- + +Programatic means that these are generally not an LLM call rather something more standard like calling an API, +transforming the data, etc. + +{% for component in pc %} +* `{{ component.id }}` - {{ component.description }} +{% endfor %} + +AI Action Components +-------------------- + +These actions generally take the input, create a custom prompt, call the Model and respond back with the result. + +{% for component in ac %} +* `{{ component.id }}` - {{ component.description }} +{% endfor %} + +Memory Components +----------------- + +Memory components are used to store data, which can be a Vector DB or Redis, etc. + +{% for component in mc %} +* `{{ component.id }}` - {{ component.description }} +{% endfor %} + +Model Components +---------------- + +Model are the different GenAI models that can be used from the ``chainfury``. + +{% for component in moc %} +* `{{ component.id }}` - {{ component.description }} +{% endfor %} + +.. all the links are here + +.. _components page: https://qdrant.tech/documentation/tutorials/bulk-upload/#upload-directly-to-disk + diff --git a/cf_internal b/cf_internal index be5b3779..5738dec3 160000 --- a/cf_internal +++ b/cf_internal @@ -1 +1 @@ -Subproject commit be5b377978257b95a2cdd7ebff709533825fc20c +Subproject commit 5738dec340709c952372dbff6c3b3505bd40e6d2 diff --git a/chainfury/agent.py b/chainfury/agent.py index 013efcd8..a082e976 100644 --- a/chainfury/agent.py +++ b/chainfury/agent.py @@ -695,7 +695,7 @@ def __call__(self, **data: Dict[str, Any]) -> Any: class MemoryRegistry: def __init__(self) -> None: - self._memories = {} + self._memories: Dict[str, Node] = {} def register_write( self, diff --git a/chainfury/base.py b/chainfury/base.py index c9c11fc3..d8aaf37c 100644 --- a/chainfury/base.py +++ b/chainfury/base.py @@ -577,7 +577,7 @@ def __init__( collection_name: str, id: str, fn: object, - description, + description: str = "", usage: List[Union[str, int]] = [], tags=[], ): diff --git a/chainfury/components/qdrant/__init__.py b/chainfury/components/qdrant/__init__.py index 209394dc..55ac0b96 100644 --- a/chainfury/components/qdrant/__init__.py +++ b/chainfury/components/qdrant/__init__.py @@ -143,6 +143,7 @@ def _insert(): fn=qdrant_write, outputs={"status": 0}, vector_key="embeddings", + description="Write to the Qdrant DB using the Qdrant client", ) @@ -251,6 +252,7 @@ def qdrant_read( fn=qdrant_read, outputs={"items": 0}, vector_key="embeddings", + description="Function to read from the Qdrant DB using the Qdrant client", ) diff --git a/scripts/list_builtins.py b/scripts/list_builtins.py new file mode 100644 index 00000000..e6afa4b3 --- /dev/null +++ b/scripts/list_builtins.py @@ -0,0 +1,58 @@ +from fire import Fire +import jinja2 as j2 +from chainfury import programatic_actions_registry, ai_actions_registry, memory_registry, model_registry + + +def main(src_file: str, trg_file: str, v: bool = False): + with open(src_file, "r") as f: + temp = j2.Template(f.read()) + + # create the components list + pc = [] + for node_id, node in programatic_actions_registry.nodes.items(): + pc.append( + { + "id": node.id, + "description": node.description.rstrip(".") + f'. Copy: ``programatic_actions_registry.get("{node.id}")``', + } + ) + + ac = [] + for node_id, node in ai_actions_registry.nodes.items(): + ac.append( + { + "id": node.id, + "description": node.description.rstrip(".") + f'. Copy: ``ai_actions_registry.get("{node.id}")``', + } + ) + + mc = [] + for node_id, node in memory_registry._memories.items(): + fn = "get_read" if node.id.endswith("-read") else "get_write" + mc.append( + { + "id": node.id, + "description": node.description.rstrip(".") + f'. Copy: ``memory_registry.{fn}("{node.id}")``', + } + ) + + moc = [] + for model_id, model in model_registry.models.items(): + moc.append( + { + "id": model_id, + "description": model.description.rstrip(".") + f'. Copy: ``model_registry.get("{model_id}")``', + } + ) + + op = temp.render(pc=pc, ac=ac, mc=mc, moc=moc) + if v: + print(op) + print("Writing to", trg_file) + + with open(trg_file, "w") as f: + f.write(op) + + +if __name__ == "__main__": + Fire(main)