У меня есть этот код, который предназначен для использования YoutubeChannelSearchTool с моделями с открытым исходным кодом Huggingface. Цель состоит в том, чтобы создать собственный инструмент, который будет использоваться в качестве альтернативы API OpenAI, позволяющий использовать инструмент без каких-либо затрат, обеспечивая при этом результаты, близкие к те, которые предоставлены OpenAI, я придумал этот код, который в основном представляет собой класс YoutubeChannelSearchTool с корректировками, внесенными для интеграции моделей с открытым исходным кодом.
from crewai_tools import YoutubeChannelSearchTool
from crewai_tools.adapters.embedchain_adapter import EmbedchainAdapter
from typing import Any, Optional, Type, Dict
from embedchain.models.data_type import DataType
from pydantic.v1 import BaseModel, Field
from langchain_groq import ChatGroq
from embedchain import App
from embedchain.config import AppConfig, BaseEmbedderConfig
from embedchain.embedder.huggingface import HuggingFaceEmbedder
import chromadb.utils.embedding_functions as embedding_functions
from decouple import config
import os
os.environ["HUGGINGFACE_ACCESS_TOKEN"] = config("HUGGINGFACE_ACCESS_TOKEN")
os.environ["GROQ_API_KEY"] = config("GROQ_API_KEY")
llm = ChatGroq(
temperature=0,
groq_api_key=os.getenv("GROQ_API_KEY"),
model_name="llama3-8b-8192",
max_tokens=8192,
)
class CustomRagTool(EmbedchainAdapter):
"""A custom RagTool that manages Embedchain configuration internally."""
def __init__(
self,
name: str = "Knowledge base",
description: str = "A knowledge base that can be used to answer questions.",
summarize: bool = False,
chunk_size: int = 500,
vector_store_type: str = "chromadb",
embedding_model: str = "sentence-transformers/all-MiniLM-L6-v2",
**kwargs: Dict[str, Any], # Explicit type hinting for kwargs
):
hf_config = BaseEmbedderConfig(
model=embedding_model,
api_key=os.getenv("HUGGINGFACE_ACCESS_TOKEN")
)
embedder = HuggingFaceEmbedder(config=hf_config)
app_config = AppConfig()
self.app = App(config=app_config, embedding_model=embedder)
super().__init__(embedchain_app=self.app, summarize=summarize)
api_key = os.getenv("HUGGINGFACE_ACCESS_TOKEN")
if not api_key:
raise ValueError("HUGGINGFACE_ACCESS_TOKEN environment variable not set.")
self.embedding_function = embedding_functions.HuggingFaceEmbeddingFunction(
api_key=os.getenv("HUGGINGFACE_ACCESS_TOKEN"),
model_name=embedding_model
)
class FixedYoutubeChannelSearchToolSchema(BaseModel):
"""Input for YoutubeChannelSearchTool."""
search_query: str = Field(
..., description="Mandatory search query you want to use to search the YouTube Channel's content"
)
class YoutubeChannelSearchToolSchema(FixedYoutubeChannelSearchToolSchema):
"""Input for YoutubeChannelSearchTool."""
youtube_channel_handle: str = Field(..., description="Mandatory YouTube channel handle path you want to search")
class CustomYoutubeChannelSearchTool(CustomRagTool):
name: str = "Search a YouTube Channel's content"
description: str = "A tool that can be used to semantically search a query from a YouTube Channel's content."
args_schema: Type[BaseModel] = YoutubeChannelSearchToolSchema
def __init__(self, youtube_channel_handle: Optional[str] = None, **kwargs):
super().__init__(**kwargs)
if youtube_channel_handle is not None:
self.add(youtube_channel_handle)
self.description = f"A tool that can be used to semantic search a query the {youtube_channel_handle} YouTube Channel's content."
self.args_schema = FixedYoutubeChannelSearchToolSchema
try:
if not super().app.db.get_collection(youtube_channel_handle):
super().app.db.add_collection(youtube_channel_handle)
except Exception as e:
raise RuntimeError(f"Error adding collection: {e}")
embedding_function = embedding_functions.HuggingFaceEmbeddingFunction(
api_key=os.getenv("HUGGINGFACE_ACCESS_TOKEN"),
model_name=embedding_model
)
collection = super().app.db.get_collection(youtube_channel_handle)
collection.set_embedding_function(embedding_function)
def add(self, youtube_channel_handle: str, **kwargs: Any) -> None:
if not youtube_channel_handle.startswith("@"):
youtube_channel_handle = f"@{youtube_channel_handle}"
kwargs["data_type"] = DataType.YOUTUBE_CHANNEL
self.app.add(youtube_channel_handle, **kwargs)
def _before_run(self, query: str, **kwargs: Any) -> Any:
if "youtube_channel_handle" in kwargs:
self.add(kwargs["youtube_channel_handle"])
def _run(self, search_query: str, **kwargs: Any) -> Any:
return super()._run(query=search_query)
# Initialize the tool
yt_search_tool = None
try:
yt_search_tool = CustomYoutubeChannelSearchTool(
youtube_channel_handle="@1littlecoder"
)
except Exception as e:
print(f"Error initializing tool: {e}")
# Verify if __init__ method is being called
if yt_search_tool is not None:
print("CustomYoutubeChannelSearchTool instance created successfully.")
else:
print("CustomYoutubeChannelSearchTool initialization failed.")
# Ask a question - add a check if initialization was successful
if yt_search_tool is not None:
query = "What are the best Python tutorials on this channel?"
response = yt_search_tool._run(query)
print(response)
else:
print("Tool initialization failed, cannot process query.")
Я получаю сообщение об ошибке и не могу понять, в чем проблема, чтобы ее решить:
Error initializing tool: "CustomYoutubeChannelSearchTool" object has no field "app"
CustomYoutubeChannelSearchTool initialization failed.
Tool initialization failed, cannot process query.
Подробнее здесь: https://stackoverflow.com/questions/785 ... openai-api