🔨 refactor(conftest.py): reformat code for better readability and maintainability
✨ feat(conftest.py): add MyCustomChain class as an example of a custom chain ✨ feat(conftest.py): add CustomChain class as a custom component for building a document ✨ feat(conftest.py): add CSVLoaderComponent class as a custom component for loading CSV files and converting rows to documents
This commit is contained in:
parent
10c0b3871c
commit
99ef7c728d
1 changed files with 120 additions and 90 deletions
|
|
@ -122,20 +122,20 @@ def client_fixture(session: Session): #
|
||||||
def custom_chain():
|
def custom_chain():
|
||||||
return '''from __future__ import annotations
|
return '''from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any, Dict, List, Optional
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
from pydantic import Extra
|
from pydantic import Extra
|
||||||
|
|
||||||
from langchain.schema import BaseLanguageModel, Document
|
from langchain.schema import BaseLanguageModel, Document
|
||||||
from langchain.callbacks.manager import (
|
from langchain.callbacks.manager import (
|
||||||
AsyncCallbackManagerForChainRun,
|
AsyncCallbackManagerForChainRun,
|
||||||
CallbackManagerForChainRun,
|
CallbackManagerForChainRun,
|
||||||
)
|
)
|
||||||
from langchain.chains.base import Chain
|
from langchain.chains.base import Chain
|
||||||
from langchain.prompts import StringPromptTemplate
|
from langchain.prompts import StringPromptTemplate
|
||||||
from langflow.interface.custom.base import CustomComponent
|
from langflow.interface.custom.base import CustomComponent
|
||||||
|
|
||||||
class MyCustomChain(Chain):
|
class MyCustomChain(Chain):
|
||||||
"""
|
"""
|
||||||
An example of a custom chain.
|
An example of a custom chain.
|
||||||
"""
|
"""
|
||||||
|
|
@ -225,7 +225,7 @@ def custom_chain():
|
||||||
def _chain_type(self) -> str:
|
def _chain_type(self) -> str:
|
||||||
return "my_custom_chain"
|
return "my_custom_chain"
|
||||||
|
|
||||||
class CustomChain(CustomComponent):
|
class CustomChain(CustomComponent):
|
||||||
display_name: str = "Custom Chain"
|
display_name: str = "Custom Chain"
|
||||||
field_config = {
|
field_config = {
|
||||||
"prompt": {"field_type": "prompt"},
|
"prompt": {"field_type": "prompt"},
|
||||||
|
|
@ -235,3 +235,33 @@ def custom_chain():
|
||||||
def build(self, prompt, llm, input: str) -> Document:
|
def build(self, prompt, llm, input: str) -> Document:
|
||||||
chain = MyCustomChain(prompt=prompt, llm=llm)
|
chain = MyCustomChain(prompt=prompt, llm=llm)
|
||||||
return chain(input)'''
|
return chain(input)'''
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def data_processing():
|
||||||
|
return """import pandas as pd
|
||||||
|
from langchain.schema import Document
|
||||||
|
from langflow.interface.custom.base import CustomComponent
|
||||||
|
|
||||||
|
class CSVLoaderComponent(CustomComponent):
|
||||||
|
display_name: str = "CSV Loader"
|
||||||
|
field_config = {
|
||||||
|
"filename": {"field_type": "str", "required": True},
|
||||||
|
"column_name": {"field_type": "str", "required": True},
|
||||||
|
}
|
||||||
|
|
||||||
|
def build(self, filename: str, column_name: str) -> List[Document]:
|
||||||
|
# Load the CSV file
|
||||||
|
df = pd.read_csv(filename)
|
||||||
|
|
||||||
|
# Verify the column exists
|
||||||
|
if column_name not in df.columns:
|
||||||
|
raise ValueError(f"Column '{column_name}' not found in the CSV file")
|
||||||
|
|
||||||
|
# Convert each row of the specified column to a document object
|
||||||
|
documents = []
|
||||||
|
for content in df[column_name]:
|
||||||
|
metadata = {"filename": filename}
|
||||||
|
documents.append(Document(page_content=str(content), metadata=metadata))
|
||||||
|
|
||||||
|
return documents"""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue