From 642acf817299428775879eadc1b4cdb554d8ea98 Mon Sep 17 00:00:00 2001 From: ogabrielluiz Date: Wed, 29 May 2024 10:09:45 -0300 Subject: [PATCH 1/2] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20(Qdrant.py):=20refacto?= =?UTF-8?q?r=20QdrantComponent=20class=20to=20improve=20code=20readability?= =?UTF-8?q?=20and=20maintainability=20by=20reordering=20and=20organizing?= =?UTF-8?q?=20the=20initialization=20parameters.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/langflow/components/vectorstores/Qdrant.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/backend/base/langflow/components/vectorstores/Qdrant.py b/src/backend/base/langflow/components/vectorstores/Qdrant.py index dabaa17fc..794e282db 100644 --- a/src/backend/base/langflow/components/vectorstores/Qdrant.py +++ b/src/backend/base/langflow/components/vectorstores/Qdrant.py @@ -67,22 +67,19 @@ class QdrantComponent(CustomComponent): documents.append(_input.to_lc_document()) else: documents.append(_input) - if documents is None: + if not documents: from qdrant_client import QdrantClient client = QdrantClient( location=location, - url=host, + url=url, port=port, grpc_port=grpc_port, https=https, prefix=prefix, timeout=timeout, prefer_grpc=prefer_grpc, - metadata_payload_key=metadata_payload_key, - content_payload_key=content_payload_key, api_key=api_key, - collection_name=collection_name, host=host, path=path, ) @@ -90,6 +87,8 @@ class QdrantComponent(CustomComponent): client=client, collection_name=collection_name, embeddings=embedding, + content_payload_key=content_payload_key, + metadata_payload_key=metadata_payload_key, ) return vs else: From 32e8da3bf407bbba4d78a396808a8a2f25d5fbcb Mon Sep 17 00:00:00 2001 From: ogabrielluiz Date: Wed, 29 May 2024 10:33:12 -0300 Subject: [PATCH 2/2] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20(OllamaModel.py):=20Re?= =?UTF-8?q?move=20unused=20imports=20and=20clean=20up=20code=20formatting?= =?UTF-8?q?=20for=20better=20readability=20=F0=9F=93=9D=20(OllamaModel.py)?= =?UTF-8?q?:=20Update=20comments=20and=20docstrings=20for=20clarity=20and?= =?UTF-8?q?=20consistency=20=F0=9F=94=A7=20(OllamaModel.py):=20Refactor=20?= =?UTF-8?q?the=20logic=20for=20setting=20the=20base=20URL=20in=20the=20get?= =?UTF-8?q?=5Fmodel=20method=20to=20improve=20maintainability=20and=20read?= =?UTF-8?q?ability?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../langflow/components/models/OllamaModel.py | 73 +++++-------------- 1 file changed, 17 insertions(+), 56 deletions(-) diff --git a/src/backend/base/langflow/components/models/OllamaModel.py b/src/backend/base/langflow/components/models/OllamaModel.py index 5806ad770..f591e4a5c 100644 --- a/src/backend/base/langflow/components/models/OllamaModel.py +++ b/src/backend/base/langflow/components/models/OllamaModel.py @@ -1,21 +1,11 @@ - -from typing import Any, Dict, List, Optional, Union - - - -from langchain_community.chat_models.ollama import ChatOllama -from langflow.base.constants import STREAM_INFO_TEXT -from langflow.base.models.model import LCModelComponent -from langchain_core.caches import BaseCache - -from langflow.field_typing import Text - - -import asyncio -import json +from typing import Any, Dict, List, Optional import httpx +from langchain_community.chat_models.ollama import ChatOllama +from langflow.base.constants import STREAM_INFO_TEXT +from langflow.base.models.model import LCModelComponent +from langflow.field_typing import Text class ChatOllamaComponent(LCModelComponent): @@ -26,18 +16,12 @@ class ChatOllamaComponent(LCModelComponent): field_order = [ "base_url", "headers", - "keep_alive_flag", "keep_alive", - "metadata", "model", - - "temperature", "cache", - - "format", "metadata", "mirostat", @@ -67,10 +51,7 @@ class ChatOllamaComponent(LCModelComponent): "base_url": { "display_name": "Base URL", "info": "Endpoint of the Ollama API. Defaults to 'http://localhost:11434' if not specified.", - }, - - "format": { "display_name": "Format", "info": "Specify the format of the output (e.g., json)", @@ -79,13 +60,10 @@ class ChatOllamaComponent(LCModelComponent): "headers": { "display_name": "Headers", "advanced": True, - - }, - "keep_alive_flag": { "display_name": "Unload interval", - "options": ["Keep", "Immediately","Minute", "Hour", "sec" ], + "options": ["Keep", "Immediately", "Minute", "Hour", "sec"], "real_time_refresh": True, "refresh_button": True, }, @@ -93,9 +71,6 @@ class ChatOllamaComponent(LCModelComponent): "display_name": "interval", "info": "How long the model will stay loaded into memory.", }, - - - "model": { "display_name": "Model Name", "options": [], @@ -109,14 +84,6 @@ class ChatOllamaComponent(LCModelComponent): "value": 0.8, "info": "Controls the creativity of model responses.", }, - - - "format": { - "display_name": "Format", - "field_type": "str", - "info": "Specify the format of the output (e.g., json).", - "advanced": True, - }, "metadata": { "display_name": "Metadata", "info": "Metadata to add to the run trace.", @@ -129,7 +96,6 @@ class ChatOllamaComponent(LCModelComponent): "advanced": False, "real_time_refresh": True, "refresh_button": True, - }, "mirostat_eta": { "display_name": "Mirostat Eta", @@ -260,10 +226,14 @@ class ChatOllamaComponent(LCModelComponent): build_config["mirostat_tau"]["value"] = 5 if field_name == "model": - base_url = build_config.get("base_url", {}).get( - "value", "http://localhost:11434") - build_config["model"]["options"] = self.get_model( - base_url + "/api/tags") + base_url_dict = build_config.get("base_url", {}) + base_url_load_from_db = base_url_dict.get("load_from_db", False) + base_url_value = base_url_dict.get("value") + if base_url_load_from_db: + base_url_value = self.variables(base_url_value) + elif not base_url_value: + base_url_value = "http://localhost:11434" + build_config["model"]["options"] = self.get_model(base_url_value + "/api/tags") if field_name == "keep_alive_flag": if field_value == "Keep": @@ -276,9 +246,6 @@ class ChatOllamaComponent(LCModelComponent): build_config["keep_alive"]["advanced"] = False return build_config - - - def get_model(self, url: str) -> List[str]: try: @@ -287,8 +254,7 @@ class ChatOllamaComponent(LCModelComponent): response.raise_for_status() data = response.json() - model_names = [model['name'] - for model in data.get("models", [])] + model_names = [model["name"] for model in data.get("models", [])] return model_names except Exception as e: raise ValueError("Could not retrieve models") from e @@ -299,15 +265,13 @@ class ChatOllamaComponent(LCModelComponent): base_url: Optional[str], model: str, input_value: Text, - - mirostat: Optional[str], + mirostat: Optional[str] = "Disabled", mirostat_eta: Optional[float] = None, mirostat_tau: Optional[float] = None, - repeat_last_n: Optional[int] = None, verbose: Optional[bool] = None, keep_alive: Optional[int] = None, - keep_alive_flag: Optional[str] = None, + keep_alive_flag: Optional[str] = "Keep", num_ctx: Optional[int] = None, num_gpu: Optional[int] = None, format: Optional[str] = None, @@ -326,12 +290,9 @@ class ChatOllamaComponent(LCModelComponent): stream: bool = False, system_message: Optional[str] = None, ) -> Text: - if not base_url: base_url = "http://localhost:11434" - - if keep_alive_flag == "Minute": keep_alive_instance = f"{keep_alive}m" elif keep_alive_flag == "Hour":