Best docs Cinnamon will probably ever have (#105)

This commit is contained in:
ian_Cin
2023-12-20 11:30:25 +07:00
committed by GitHub
parent 0e30dcbb06
commit 230328c62f
40 changed files with 1036 additions and 46 deletions

View File

@@ -4,6 +4,7 @@ from .base import BaseLLM
from .branching import GatedBranchingPipeline, SimpleBranchingPipeline
from .chats import AzureChatOpenAI, ChatLLM
from .completions import LLM, AzureOpenAI, OpenAI
from .cot import ManualSequentialChainOfThought, Thought
from .linear import GatedLinearPipeline, SimpleLinearPipeline
from .prompts import BasePromptComponent, PromptTemplate
@@ -28,4 +29,7 @@ __all__ = [
"GatedLinearPipeline",
"SimpleBranchingPipeline",
"GatedBranchingPipeline",
# chain-of-thoughts
"ManualSequentialChainOfThought",
"Thought",
]

View File

@@ -12,7 +12,8 @@ class SimpleBranchingPipeline(BaseComponent):
Attributes:
branches (List[BaseComponent]): The list of branches to be executed.
Example Usage:
Example:
```python
from kotaemon.llms import (
AzureChatOpenAI,
BasePromptComponent,
@@ -45,6 +46,7 @@ class SimpleBranchingPipeline(BaseComponent):
print(pipeline(condition_text="1"))
print(pipeline(condition_text="2"))
print(pipeline(condition_text="12"))
```
"""
branches: List[BaseComponent] = Param(default_callback=lambda *_: [])
@@ -87,7 +89,8 @@ class GatedBranchingPipeline(SimpleBranchingPipeline):
Attributes:
branches (List[BaseComponent]): The list of branches to be executed.
Example Usage:
Example:
```python
from kotaemon.llms import (
AzureChatOpenAI,
BasePromptComponent,
@@ -119,6 +122,7 @@ class GatedBranchingPipeline(SimpleBranchingPipeline):
)
print(pipeline(condition_text="1"))
print(pipeline(condition_text="2"))
```
"""
def run(self, *, condition_text: Optional[str] = None, **prompt_kwargs):
@@ -135,7 +139,7 @@ class GatedBranchingPipeline(SimpleBranchingPipeline):
Union[OutputType, None]: The output of the first branch that satisfies the
condition, or None if no branch satisfies the condition.
Raise:
Raises:
ValueError: If condition_text is None
"""
if condition_text is None:

View File

@@ -1,7 +1,9 @@
from copy import deepcopy
from typing import Callable, List
from kotaemon.base import BaseComponent, Document, Node, Param
from theflow import Function, Node, Param
from kotaemon.base import BaseComponent, Document
from .chats import AzureChatOpenAI
from .completions import LLM
@@ -66,13 +68,13 @@ class Thought(BaseComponent):
prompt: str = Param(
help=(
"The prompt template string. This prompt template has Python-like "
"variable placeholders, that then will be subsituted with real values when "
"this component is executed"
"The prompt template string. This prompt template has Python-like variable"
" placeholders, that then will be substituted with real values when this"
" component is executed"
)
)
llm: LLM = Node(AzureChatOpenAI, help="The LLM model to execute the input prompt")
post_process: BaseComponent = Node(
post_process: Function = Node(
help=(
"The function post-processor that post-processes LLM output prediction ."
"It should take a string as input (this is the LLM output text) and return "
@@ -83,7 +85,7 @@ class Thought(BaseComponent):
@Node.auto(depends_on="prompt")
def prompt_template(self):
"""Automatically wrap around param prompt. Can ignore"""
return BasePromptComponent(template=self.prompt)
return BasePromptComponent(self.prompt)
def run(self, **kwargs) -> Document:
"""Run the chain of thought"""
@@ -113,20 +115,19 @@ class ManualSequentialChainOfThought(BaseComponent):
**Create and run a chain of thought without "+" operator:**
```python
>> from kotaemon.pipelines.cot import Thought, ManualSequentialChainOfThought
>> llm = AzureChatOpenAI(...)
>> thought1 = Thought(
prompt="Word {word} in {language} is ",
post_process=lambda string: {"translated": string},
)
>> thought2 = Thought(
prompt="Translate {translated} to Japanese",
post_process=lambda string: {"output": string},
)
>> thought = ManualSequentialChainOfThought(thoughts=[thought1, thought2], llm=llm)
>> thought(word="hello", language="French")
```pycon
>>> from kotaemon.pipelines.cot import Thought, ManualSequentialChainOfThought
>>> llm = AzureChatOpenAI(...)
>>> thought1 = Thought(
>>> prompt="Word {word} in {language} is ",
>>> post_process=lambda string: {"translated": string},
>>> )
>>> thought2 = Thought(
>>> prompt="Translate {translated} to Japanese",
>>> post_process=lambda string: {"output": string},
>>> )
>>> thought = ManualSequentialChainOfThought(thoughts=[thought1, thought2], llm=llm)
>>> thought(word="hello", language="French")
{'word': 'hello',
'language': 'French',
'translated': '"Bonjour"',

View File

@@ -21,6 +21,7 @@ class SimpleLinearPipeline(BaseComponent):
post-processor component or function.
Example Usage:
```python
from kotaemon.llms import AzureChatOpenAI, BasePromptComponent
def identity(x):
@@ -41,6 +42,7 @@ class SimpleLinearPipeline(BaseComponent):
post_processor=identity,
)
print(pipeline(word="lone"))
```
"""
prompt: BasePromptComponent
@@ -85,7 +87,8 @@ class GatedLinearPipeline(SimpleLinearPipeline):
condition (Callable[[IO_Type], Any]): A callable function that represents the
condition.
Example Usage:
Usage:
```{.py3 title="Example Usage"}
from kotaemon.llms import AzureChatOpenAI, BasePromptComponent
from kotaemon.parsers import RegexExtractor
@@ -109,6 +112,7 @@ class GatedLinearPipeline(SimpleLinearPipeline):
)
print(pipeline(condition_text="some pattern", word="lone"))
print(pipeline(condition_text="other pattern", word="lone"))
```
"""
condition: Callable[[IO_Type], Any]

View File

@@ -72,7 +72,7 @@ class PromptTemplate:
UserWarning,
)
def populate(self, **kwargs):
def populate(self, **kwargs) -> str:
"""
Strictly populate the template with the given keyword arguments.
@@ -81,7 +81,7 @@ class PromptTemplate:
Each keyword corresponds to a placeholder in the template.
Returns:
str: The populated template.
The populated template.
Raises:
ValueError: If an unknown placeholder is provided.