Help with something I think should be easy? #3006
Replies: 1 comment
|
Hey @holdengand! I'm here to help you with any bugs, questions, and becoming a contributor. Let's tackle this together! To automate the process of parsing raw data, comparing it to a predefined JSON structure, extracting relevant data, and outputting it in the desired JSON format using LangFlow and Ollama, you can use the
Here is an example of how you can use the from langflow.custom import Component
from langflow.helpers.data import data_to_text
from langflow.io import DataInput, MultilineInput, Output, StrInput
from langflow.schema.message import Message
class ParseDataComponent(Component):
display_name = "Parse Data"
description = "Convert Data into plain text following a specified template."
icon = "braces"
inputs = [
DataInput(name="data", display_name="Data", info="The data to convert to text."),
MultilineInput(
name="template",
display_name="Template",
info="The template to use for formatting the data. It can contain the keys {text}, {data} or any other key in the Data.",
value="{text}",
),
StrInput(name="sep", display_name="Separator", advanced=True, value="\n"),
]
outputs = [
Output(display_name="Text", name="text", method="parse_data"),
]
def parse_data(self) -> Message:
data = self.data if isinstance(self.data, list) else [self.data]
template = self.template
result_string = data_to_text(template, data, sep=self.sep)
self.status = result_string
return Message(text=result_string)
# Example usage
data = {
"name": "John Doe",
"age": 30,
"email": "john.doe@example.com"
}
template = "Name: {name}\nAge: {age}\nEmail: {email}"
component = ParseDataComponent()
component.data = data
component.template = template
component.sep = "\n"
message = component.parse_data()
print(message.text)This code will parse the raw data according to the provided template and output it in the desired format. You can adjust the Additionally, you can use the from langflow import CustomComponent
from langchain_community.llms.ollama import Ollama
from langchain.llms.base import BaseLLM
from typing import List, Optional
class OllamaLLM(CustomComponent):
display_name = "Ollama"
description = "Local LLM with Ollama."
def build_config(self) -> dict:
return {
"base_url": {
"display_name": "Base URL",
"info": "Endpoint of the Ollama API. Defaults to 'http://localhost:11434' if not specified.",
},
"model": {
"display_name": "Model Name",
"value": "llama2",
"info": "Refer to https://ollama.ai/library for more models.",
},
"temperature": {
"display_name": "Temperature",
"field_type": "float",
"value": 0.8,
"info": "Controls the creativity of model responses.",
},
"mirostat": {
"display_name": "Mirostat",
"options": ["Disabled", "Mirostat", "Mirostat 2.0"],
"info": "Enable/disable Mirostat sampling for controlling perplexity.",
"value": "Disabled",
"advanced": True,
},
"mirostat_eta": {
"display_name": "Mirostat Eta",
"field_type": "float",
"info": "Learning rate influencing the algorithm's response to feedback.",
"advanced": True,
},
"mirostat_tau": {
"display_name": "Mirostat Tau",
"field_type": "float",
"info": "Controls balance between coherence and diversity.",
"advanced": True,
},
"num_ctx": {
"display_name": "Context Window Size",
"field_type": "int",
"info": "Size of the context window for generating the next token.",
"advanced": True,
},
"num_gpu": {
"display_name": "Number of GPUs",
"field_type": "int",
"info": "Number of GPUs to use for computation.",
"advanced": True,
},
"num_thread": {
"display_name": "Number of Threads",
"field_type": "int",
"info": "Number of threads to use during computation.",
"advanced": True,
},
"repeat_last_n": {
"display_name": "Repeat Last N",
"field_type": "int",
"info": "Sets how far back the model looks to prevent repetition.",
"advanced": True,
},
"repeat_penalty": {
"display_name": "Repeat Penalty",
"field_type": "float",
"info": "Penalty for repetitions in generated text.",
"advanced": True,
},
"stop": {
"display_name": "Stop Tokens",
"info": "List of tokens to signal the model to stop generating text.",
"advanced": True,
},
"tfs_z": {
"display_name": "TFS Z",
"field_type": "float",
"info": "Tail free sampling to reduce impact of less probable tokens.",
"advanced": True,
},
"top_k": {
"display_name": "Top K",
"field_type": "int",
"info": "Limits token selection to top K for reducing nonsense generation.",
"advanced": True,
},
"top_p": {
"display_name": "Top P",
"field_type": "int",
"info": "Works with top-k to control diversity of generated text.",
"advanced": True,
},
}
def build(
self,
base_url: Optional[str],
model: str,
temperature: Optional[float],
mirostat: Optional[str],
mirostat_eta: Optional[float] = None,
mirostat_tau: Optional[float] = None,
num_ctx: Optional[int] = None,
num_gpu: Optional[int] = None,
num_thread: Optional[int] = None,
repeat_last_n: Optional[int] = None,
repeat_penalty: Optional[float] = None,
stop: Optional[List[str]] = None,
tfs_z: Optional[float] = None,
top_k: Optional[int] = None,
top_p: Optional[int] = None,
) -> BaseLLM:
if not base_url:
base_url = "http://localhost:11434"
# Mapping mirostat settings to their corresponding values
mirostat_options = {"Mirostat": 1, "Mirostat 2.0": 2}
# Default to 0 for 'Disabled'
mirostat_value = mirostat_options.get(mirostat, 0) # type: ignore
# Set mirostat_eta and mirostat_tau to None if mirostat is disabled
if mirostat_value == 0:
mirostat_eta = None
mirostat_tau = None
try:
llm = Ollama(
base_url=base_url,
model=model,
mirostat=mirostat_value,
mirostat_eta=mirostat_eta,
mirostat_tau=mirostat_tau,
num_ctx=num_ctx,
num_gpu=num_gpu,
num_thread=num_thread,
repeat_last_n=repeat_last_n,
repeat_penalty=repeat_penalty,
temperature=temperature,
stop=stop,
tfs_z=tfs_z,
top_k=top_k,
top_p=top_p,
)
except Exception as e:
raise ValueError("Could not connect to Ollama.") from e
return llmBy combining these components, you can create a comprehensive workflow in LangFlow to parse, validate, and format your data as needed. Additionally, you can configure advanced settings such as default headers, query parameters, and other options to fine-tune the API requests and model behavior [1][2]. |
Uh oh!
There was an error while loading. Please reload this page.
Hi, I'm willing to preface this and say I am new to agents and workflows when it comes to LLMs. Also that I'm not a programmer and am a fairly casual user of AI in general, so take it easy on me.
I have a task I expect to need to do hundreds of times and I think should be easy to implement, but I'm having trouble figuring out exactly what tool I need to actually parse my data for the end results.
I am needing to dump raw data in, have it compared a predefined json formatted structure, determine the relevant data from the raw data for the fields in the predefined format and the output the result with is the details of that data restructured into that predefined json format. And ideally inputting something like 'Null' or similar in fields missing data so that missing data can be easily located later. I was previously doing this with LLMs without agents or workflows and it 'worked' but was of course producing inconsistent results. After spending the past two days researching and looking into solutions to improve accuracy so I didn't have to micromanage and continuously fix data and reformat, I'm now here. I have played with the LangFlow interface for a while, and after looking at almost every component I'm just not sure what I would need to do to enforce the formatting I'm trying to do (I thought 'parse data' would do it but it doesn't seem to like the json format).
I'll also point out I am trying to do this all locally, so not dealing with any APIs or anything; and using ollama for any model access on LangFlow atm.
I did skim the discussions, and store page, and it is very possible I overlooked related information or perhaps didn't even realize I looked at the solution and simply misunderstood.
Any advise or examples of similar workflows?
All reactions