Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Ollama base url handling and Qdrant component #2007

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 15 additions & 54 deletions src/backend/base/langflow/components/models/OllamaModel.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
from typing import Any, Dict, List, Optional

from typing import Any, Dict, List, Optional, Union



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 langchain_core.caches import BaseCache

from langflow.field_typing import Text


import asyncio
import json

import httpx



class ChatOllamaComponent(LCModelComponent):
display_name = "Ollama"
description = "Generate text using Ollama Local LLMs."
Expand All @@ -26,18 +16,12 @@ class ChatOllamaComponent(LCModelComponent):
field_order = [
"base_url",
"headers",

"keep_alive_flag",
"keep_alive",

"metadata",
"model",


"temperature",
"cache",


"format",
"metadata",
"mirostat",
Expand Down Expand Up @@ -67,10 +51,7 @@ def build_config(self) -> dict:
"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)",
Expand All @@ -79,23 +60,17 @@ def build_config(self) -> dict:
"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,
},
"keep_alive": {
"display_name": "interval",
"info": "How long the model will stay loaded into memory.",
},



"model": {
"display_name": "Model Name",
"options": [],
Expand All @@ -109,14 +84,6 @@ def build_config(self) -> dict:
"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.",
Expand All @@ -129,7 +96,6 @@ def build_config(self) -> dict:
"advanced": False,
"real_time_refresh": True,
"refresh_button": True,

},
"mirostat_eta": {
"display_name": "Mirostat Eta",
Expand Down Expand Up @@ -260,10 +226,14 @@ def update_build_config(self, build_config: dict, field_value: Any, field_name:
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":
Expand All @@ -276,9 +246,6 @@ def update_build_config(self, build_config: dict, field_value: Any, field_name:
build_config["keep_alive"]["advanced"] = False

return build_config




def get_model(self, url: str) -> List[str]:
try:
Expand All @@ -287,8 +254,7 @@ def get_model(self, url: str) -> List[str]:
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
Expand All @@ -299,15 +265,13 @@ def build(
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,
Expand All @@ -326,12 +290,9 @@ def build(
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":
Expand Down
9 changes: 4 additions & 5 deletions src/backend/base/langflow/components/vectorstores/Qdrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,28 @@ def build(
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,
)
vs = Qdrant(
client=client,
collection_name=collection_name,
embeddings=embedding,
content_payload_key=content_payload_key,
metadata_payload_key=metadata_payload_key,
)
return vs
else:
Expand Down