Skip to content

Commit

Permalink
Merge pull request #64 from SylphAI-Inc/li
Browse files Browse the repository at this point in the history
Li
  • Loading branch information
liyin2015 authored Jul 1, 2024
2 parents 632e1b1 + 6dca9c4 commit 5071653
Show file tree
Hide file tree
Showing 18 changed files with 320 additions and 176 deletions.
84 changes: 84 additions & 0 deletions developer_notes/prompt_note.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
def python_str_format_example(task_desc_str: str, input_str: str):

# percent(%) formatting
print("<SYS>%s</SYS> User: %s" % (task_desc_str, input_str))

# format() method with kwargs
print(
"<SYS>{task_desc_str}</SYS> User: {input_str}".format(
task_desc_str=task_desc_str, input_str=input_str
)
)

# f-string
print(f"<SYS>{task_desc_str}</SYS> User: {input_str}")

# Templates
from string import Template

t = Template("<SYS>$task_desc_str</SYS> User: $input_str")
print(t.substitute(task_desc_str=task_desc_str, input_str=input_str))


def jinja2_template_example(template, **kwargs):
from jinja2 import Template

t = Template(template, trim_blocks=True, lstrip_blocks=True)
print(t.render(**kwargs))


def lightrag_prompt(template, task_desc_str, input_str, tools=None):
from lightrag.core.prompt_builder import Prompt

prompt = Prompt(
template=template,
prompt_kwargs={
"task_desc_str": task_desc_str,
"tools": tools,
},
)
print(prompt)
print(prompt(input_str=input_str))

saved_prompt = prompt.to_dict()
restored_prompt = Prompt.from_dict(saved_prompt)
print(
restored_prompt == prompt
) # False as the jinja2 template can not be serialized, but we recreated the template from the string at the time of restoration, so it works the same
print(restored_prompt)


def lightrag_default_prompt():
from lightrag.core.prompt_builder import Prompt

prompt = Prompt()
input_str = "What is the capital of France?"
output = prompt(input_str=input_str)
print(output)


if __name__ == "__main__":

task_desc_str = "You are a helpful assitant"
input_str = "What is the capital of France?"
tools = ["google", "wikipedia", "wikidata"]
template = r"""<SYS>{{ task_desc_str }}</SYS>
{# tools #}
{% if tools %}
<TOOLS>
{% for tool in tools %}
{{loop.index}}. {{ tool }}
{% endfor %}
</TOOLS>
{% endif %}
User: {{ input_str }}"""
python_str_format_example(task_desc_str, input_str)
jinja2_template_example(template, task_desc_str=task_desc_str, input_str=input_str)
jinja2_template_example(
template, task_desc_str=task_desc_str, input_str=input_str, tools=tools
)
lightrag_prompt(
template, task_desc_str=task_desc_str, input_str=input_str, tools=tools
)

lightrag_default_prompt()
Binary file removed docs/source/_static/database.png
Binary file not shown.
7 changes: 4 additions & 3 deletions docs/source/apis/components/index.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _apis-components:

Components
==============

Expand All @@ -10,9 +12,9 @@ Overview
components.agent
components.model_client
components.data_process

.. components.reasoning
components.retriever
components.output_parsers

Expand Down Expand Up @@ -65,4 +67,3 @@ Retrievers
:maxdepth: 1

components.retriever

4 changes: 3 additions & 1 deletion docs/source/apis/core/index.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _apis-core:

Core
===================

Expand All @@ -7,7 +9,7 @@ Overview
----------
.. autosummary::

core.base_data_class
core.base_data_class
core.component
core.db
core.default_prompt_template
Expand Down
2 changes: 2 additions & 0 deletions docs/source/apis/eval/index.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _apis-eval:

Evaluation
==============

Expand Down
4 changes: 3 additions & 1 deletion docs/source/apis/optim/index.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _apis-optim:

.. Optimizer
.. ==============
Expand All @@ -22,4 +24,4 @@ Optimizer
optim.sampler
optim.few_shot_optimizer
optim.llm_augment
optim.llm_optimizer
optim.llm_optimizer
4 changes: 3 additions & 1 deletion docs/source/apis/tracing/index.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _apis-tracing:

Tracing
==============

Expand All @@ -22,4 +24,4 @@ Loggers
:maxdepth: 1

tracing.generator_state_logger
tracing.generator_call_logger
tracing.generator_call_logger
3 changes: 2 additions & 1 deletion docs/source/apis/utils/index.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _apis-utils:

Utils
=============================

Expand Down Expand Up @@ -31,4 +33,3 @@ Setup_env
:maxdepth: 1

utils.setup_env

20 changes: 12 additions & 8 deletions docs/source/developer_notes/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Tutorials
=============================

*Why and How Each Part works*
.. *Why and How Each Part works*
Learn the `why` and `how-to` (customize and integrate) behind each core part within the `LightRAG` library.
These are our most important tutorials before you move ahead to build use cases (LLM applications) end to end.
Expand Down Expand Up @@ -47,7 +47,8 @@ Building
-------------------
Base classes
~~~~~~~~~~~~~~~~~~~~~~
Code path: ``lightrag.core``.
Code path: :ref:`lightrag.core <apis-core>`.


.. list-table::
:widths: 20 80
Expand All @@ -56,9 +57,9 @@ Code path: ``lightrag.core``.
* - Base Class
- Description
* - :doc:`component`
- Similar to ``Module`` in `PyTorch`, it standardizes the interface of all components with `call`, `acall`, and `__call__` methods, handles states, and serialization. Components can be easily chained togehter via `Sequential` for now.
- The building block for task pipeline. It standardizes the interface of all components with `call`, `acall`, and `__call__` methods, handles state serialization, nested components, and parameters for optimization. Components can be easily chained together via ``Sequential``.
* - :doc:`base_data_class`
- Leverages the ``dataclasses`` module in Python to ease the data interaction with prompt and serialization.
- The base class for data. It eases the data interaction with LLMs for both prompt formatting and output parsing.



Expand All @@ -79,10 +80,10 @@ RAG components
^^^^^^^^^^^^^^^^^^^


Code path: ``lightrag.core``. For abstract classes:
Code path: :ref:`lightrag.core<apis-core>`. For abstract classes:

- ``ModelClient``: the functional subclass is in ``lightrag.components.model_client``.
- ``Retriever``: the functional subclass is in ``lightrag.components.retriever``. It works hand-in-hand with the ``LocalDB`` and Cloud DB in ``lightrag.database``.
- ``ModelClient``: the functional subclass is in :ref:`lightrag.components.model_client<components-model_client>`.
- ``Retriever``: the functional subclass is in :ref:`lightrag.components.retriever<components-retriever>`.


.. list-table::
Expand All @@ -92,11 +93,13 @@ Code path: ``lightrag.core``. For abstract classes:
* - Part
- Description
* - :doc:`prompt`
- Built on ``jinja2``, it programmablly and flexibly format prompt(text) as **input to the generator**.
- Built on `jinja2`, it programmatically and flexibly formats prompts as input to the generator.
* - :doc:`model_client`
- ``ModelClient`` is the protocol and base class for LightRAG to **integrate all models**, either APIs or local, LLMs or Embedding models or any others.
* - :doc:`generator`
- The **center component** that orchestrates the model client(LLMs in particular), prompt, and output processors for format parsing or any post processing.
* - :doc:`output_parsers`
- The component that parses the output string to structured data.
* - :doc:`embedder`
- The component that orchestrates model client (Embedding models in particular) and output processors.
* - :doc:`retriever`
Expand Down Expand Up @@ -133,6 +136,7 @@ Components work on a sequence of ``Document`` and return a sequence of ``Documen
prompt
model_client
generator
output_parsers
embedder
retriever
text_splitter
Expand Down
2 changes: 2 additions & 0 deletions docs/source/developer_notes/output_parsers.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
OutputParser
=============
Loading

0 comments on commit 5071653

Please sign in to comment.