Fix import errors and type annotations

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-01-25 21:15:28 -03:00
commit 5cf56ca7a8
13 changed files with 63 additions and 103 deletions

View file

@ -1,7 +1,8 @@
from langflow import CustomComponent
from langchain.embeddings.base import Embeddings from langchain.embeddings.base import Embeddings
from langchain_community.embeddings import AzureOpenAIEmbeddings from langchain_community.embeddings import AzureOpenAIEmbeddings
from langflow import CustomComponent
class AzureOpenAIEmbeddingsComponent(CustomComponent): class AzureOpenAIEmbeddingsComponent(CustomComponent):
display_name: str = "AzureOpenAIEmbeddings" display_name: str = "AzureOpenAIEmbeddings"
@ -53,9 +54,9 @@ class AzureOpenAIEmbeddingsComponent(CustomComponent):
try: try:
embeddings = AzureOpenAIEmbeddings( embeddings = AzureOpenAIEmbeddings(
azure_endpoint=azure_endpoint, azure_endpoint=azure_endpoint,
deployment=azure_deployment, azure_deployment=azure_deployment,
openai_api_version=api_version, api_version=api_version,
openai_api_key=api_key, api_key=api_key,
) )
except Exception as e: except Exception as e:

View file

@ -1,7 +1,6 @@
from typing import Optional from typing import Optional
from langchain_community.embeddings.cohere import CohereEmbeddings from langchain_community.embeddings.cohere import CohereEmbeddings
from langflow import CustomComponent from langflow import CustomComponent
@ -27,7 +26,7 @@ class CohereEmbeddingsComponent(CustomComponent):
truncate: Optional[str] = None, truncate: Optional[str] = None,
user_agent: str = "langchain", user_agent: str = "langchain",
) -> CohereEmbeddings: ) -> CohereEmbeddings:
return CohereEmbeddings( return CohereEmbeddings( # type: ignore
max_retries=max_retries, max_retries=max_retries,
user_agent=user_agent, user_agent=user_agent,
request_timeout=request_timeout, request_timeout=request_timeout,

View file

@ -1,7 +1,9 @@
from typing import Any, Callable, Dict, List, Optional, Union
from langchain_openai.embeddings.base import OpenAIEmbeddings
from langflow import CustomComponent from langflow import CustomComponent
from langflow.field_typing import NestedDict from langflow.field_typing import NestedDict
from typing import List, Optional, Dict, Any, Union, Callable
from langchain_openai.embeddings.base import OpenAIEmbeddings
class OpenAIEmbeddingsComponent(CustomComponent): class OpenAIEmbeddingsComponent(CustomComponent):
@ -102,13 +104,13 @@ class OpenAIEmbeddingsComponent(CustomComponent):
max_retries=max_retries, max_retries=max_retries,
model=model, model=model,
model_kwargs=model_kwargs, model_kwargs=model_kwargs,
openai_api_base=openai_api_base, base_url=openai_api_base,
openai_api_key=openai_api_key, api_key=openai_api_key,
openai_api_type=openai_api_type, openai_api_type=openai_api_type,
openai_api_version=openai_api_version, api_version=openai_api_version,
openai_organization=openai_organization, organization=openai_organization,
openai_proxy=openai_proxy, openai_proxy=openai_proxy,
request_timeout=request_timeout, timeout=request_timeout,
show_progress_bar=show_progress_bar, show_progress_bar=show_progress_bar,
skip_empty=skip_empty, skip_empty=skip_empty,
tiktoken_model_name=tiktoken_model_name, tiktoken_model_name=tiktoken_model_name,

View file

@ -1,8 +1,10 @@
from pydantic import SecretStr
from langflow import CustomComponent
from typing import Optional from typing import Optional
from langflow.field_typing import BaseLanguageModel, NestedDict
from langchain_community.llms.anthropic import Anthropic from langchain_community.llms.anthropic import Anthropic
from pydantic.v1 import SecretStr
from langflow import CustomComponent
from langflow.field_typing import BaseLanguageModel, NestedDict
class AnthropicComponent(CustomComponent): class AnthropicComponent(CustomComponent):

View file

@ -1,6 +1,8 @@
from langflow import CustomComponent from typing import Dict, Optional
from langchain_community.llms.ctransformers import CTransformers from langchain_community.llms.ctransformers import CTransformers
from typing import Optional, Dict
from langflow import CustomComponent
class CTransformersComponent(CustomComponent): class CTransformersComponent(CustomComponent):
@ -28,4 +30,4 @@ class CTransformersComponent(CustomComponent):
} }
def build(self, model: str, model_file: str, model_type: str, config: Optional[Dict] = None) -> CTransformers: def build(self, model: str, model_file: str, model_type: str, config: Optional[Dict] = None) -> CTransformers:
return CTransformers(model=model, model_file=model_file, model_type=model_type, config=config) return CTransformers(model=model, model_file=model_file, model_type=model_type, config=config) # type: ignore

View file

@ -2,7 +2,6 @@ from typing import Optional, Union
from langchain.llms import BaseLLM from langchain.llms import BaseLLM
from langchain_community.chat_models.openai import ChatOpenAI from langchain_community.chat_models.openai import ChatOpenAI
from langflow import CustomComponent from langflow import CustomComponent
from langflow.field_typing import BaseLanguageModel, NestedDict from langflow.field_typing import BaseLanguageModel, NestedDict
@ -78,8 +77,8 @@ class ChatOpenAIComponent(CustomComponent):
return ChatOpenAI( return ChatOpenAI(
max_tokens=max_tokens, max_tokens=max_tokens,
model_kwargs=model_kwargs, model_kwargs=model_kwargs,
model_name=model_name, model=model_name,
openai_api_base=openai_api_base, base_url=openai_api_base,
openai_api_key=openai_api_key, api_key=openai_api_key,
temperature=temperature, temperature=temperature,
) )

View file

@ -1,6 +1,6 @@
from langflow import CustomComponent
from langchain_core.language_models.base import BaseLanguageModel
from langchain_community.llms.cohere import Cohere from langchain_community.llms.cohere import Cohere
from langchain_core.language_models.base import BaseLanguageModel
from langflow import CustomComponent
class CohereComponent(CustomComponent): class CohereComponent(CustomComponent):
@ -21,4 +21,4 @@ class CohereComponent(CustomComponent):
max_tokens: int = 256, max_tokens: int = 256,
temperature: float = 0.75, temperature: float = 0.75,
) -> BaseLanguageModel: ) -> BaseLanguageModel:
return Cohere(cohere_api_key=cohere_api_key, max_tokens=max_tokens, temperature=temperature) return Cohere(cohere_api_key=cohere_api_key, max_tokens=max_tokens, temperature=temperature) # type: ignore

View file

@ -1,60 +0,0 @@
from typing import Dict, Optional
from langchain_openai.llms.base import OpenAI
from langflow import CustomComponent
class OpenAIComponent(CustomComponent):
display_name = "OpenAI"
description = "OpenAI large language models."
def build_config(self):
return {
"max_tokens": {"display_name": "Max Tokens", "default": 256},
"model_kwargs": {"display_name": "Model Kwargs", "advanced": True},
"model_name": {
"display_name": "Model Name",
"value": "text-davinci-003",
"options": [
"text-davinci-003",
"text-davinci-002",
"text-curie-001",
"text-babbage-001",
"text-ada-001",
],
},
"openai_api_base": {
"display_name": "OpenAI API Base",
"info": (
"The base URL of the OpenAI API. Defaults to https://api.openai.com/v1.\n"
"You can change this to use other APIs like JinaChat, LocalAI and Prem."
),
},
"openai_api_key": {
"display_name": "OpenAI API Key",
"value": "",
"password": True,
},
"temperature": {"display_name": "Temperature", "value": 0.7},
}
def build(
self,
max_tokens: Optional[int] = 256,
model_kwargs: Optional[Dict] = None,
model_name: str = "text-davinci-003",
openai_api_base: Optional[str] = "",
openai_api_key: str = "",
temperature: Optional[float] = 0.7,
) -> OpenAI:
if not openai_api_base:
openai_api_base = "https://api.openai.com/v1"
return OpenAI(
max_tokens=max_tokens,
model_kwargs=model_kwargs or {},
model_name=model_name,
openai_api_base=openai_api_base,
openai_api_key=openai_api_key,
temperature=temperature,
)

View file

@ -1,11 +1,8 @@
from langflow import CustomComponent from typing import Callable, Optional, Union
from langchain.retrievers import MultiQueryRetriever from langchain.retrievers import MultiQueryRetriever
from typing import Optional, Union, Callable from langflow import CustomComponent
from langflow.field_typing import ( from langflow.field_typing import BaseLLM, BaseRetriever, PromptTemplate
PromptTemplate,
BaseLLM,
BaseRetriever,
)
class MultiQueryRetrieverComponent(CustomComponent): class MultiQueryRetrieverComponent(CustomComponent):
@ -46,4 +43,7 @@ class MultiQueryRetrieverComponent(CustomComponent):
prompt: Optional[PromptTemplate] = None, prompt: Optional[PromptTemplate] = None,
parser_key: str = "lines", parser_key: str = "lines",
) -> Union[Callable, MultiQueryRetriever]: ) -> Union[Callable, MultiQueryRetriever]:
return MultiQueryRetriever(llm=llm, retriever=retriever, prompt=prompt, parser_key=parser_key) if not prompt:
return MultiQueryRetriever.from_llm(llm=llm, retriever=retriever, parser_key=parser_key)
else:
return MultiQueryRetriever.from_llm(llm=llm, retriever=retriever, prompt=prompt, parser_key=parser_key)

View file

@ -1,6 +1,7 @@
from langflow import CustomComponent
from typing import Callable, Union from typing import Callable, Union
from langchain_community.utilities.serpapi import SerpAPIWrapper from langchain_community.utilities.serpapi import SerpAPIWrapper
from langflow import CustomComponent
class SerpAPIWrapperComponent(CustomComponent): class SerpAPIWrapperComponent(CustomComponent):
@ -24,7 +25,7 @@ class SerpAPIWrapperComponent(CustomComponent):
serpapi_api_key: str, serpapi_api_key: str,
params: dict, params: dict,
) -> Union[SerpAPIWrapper, Callable]: # Removed quotes around SerpAPIWrapper ) -> Union[SerpAPIWrapper, Callable]: # Removed quotes around SerpAPIWrapper
return SerpAPIWrapper( return SerpAPIWrapper( # type: ignore
serpapi_api_key=serpapi_api_key, serpapi_api_key=serpapi_api_key,
params=params, params=params,
) )

View file

@ -1,6 +1,7 @@
from langflow import CustomComponent from typing import Callable, Union
from typing import Union, Callable
from langchain_community.utilities.wikipedia import WikipediaAPIWrapper from langchain_community.utilities.wikipedia import WikipediaAPIWrapper
from langflow import CustomComponent
# Assuming WikipediaAPIWrapper is a class that needs to be imported. # Assuming WikipediaAPIWrapper is a class that needs to be imported.
# The import statement is not included as it is not provided in the JSON # The import statement is not included as it is not provided in the JSON
@ -14,5 +15,16 @@ class WikipediaAPIWrapperComponent(CustomComponent):
def build_config(self): def build_config(self):
return {} return {}
def build(self) -> Union[WikipediaAPIWrapper, Callable]: def build(
return WikipediaAPIWrapper() self,
top_k_results: int = 3,
lang: str = "en",
load_all_available_meta: bool = False,
doc_content_chars_max: int = 4000,
) -> Union[WikipediaAPIWrapper, Callable]:
return WikipediaAPIWrapper( # type: ignore
top_k_results=top_k_results,
lang=lang,
load_all_available_meta=load_all_available_meta,
doc_content_chars_max=doc_content_chars_max,
)

View file

@ -1,6 +1,8 @@
from langflow import CustomComponent
from typing import Callable, Union from typing import Callable, Union
from langchain_community.utilities.wolfram_alpha import WolframAlphaAPIWrapper from langchain_community.utilities.wolfram_alpha import WolframAlphaAPIWrapper
from langflow import CustomComponent
# Since all the fields in the JSON have show=False, we will only create a basic component # Since all the fields in the JSON have show=False, we will only create a basic component
# without any configurable fields. # without any configurable fields.
@ -13,4 +15,4 @@ class WolframAlphaAPIWrapperComponent(CustomComponent):
return {"appid": {"display_name": "App ID", "type": "str", "password": True}} return {"appid": {"display_name": "App ID", "type": "str", "password": True}}
def build(self, appid: str) -> Union[Callable, WolframAlphaAPIWrapper]: def build(self, appid: str) -> Union[Callable, WolframAlphaAPIWrapper]:
return WolframAlphaAPIWrapper(wolfram_alpha_appid=appid) return WolframAlphaAPIWrapper(wolfram_alpha_appid=appid) # type: ignore

View file

@ -7,7 +7,7 @@ from langflow.field_typing import (
) )
from langchain.schema import BaseRetriever from langchain.schema import BaseRetriever
from langchain.vectorstores.base import VectorStore from langchain.vectorstores.base import VectorStore
import pinecone import pinecone # type: ignore
class PineconeComponent(CustomComponent): class PineconeComponent(CustomComponent):