File size: 1,408 Bytes
5dde853
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
import chromadb
from llama_index.core import VectorStoreIndex
from llama_index.vector_stores.chroma import ChromaVectorStore
from llama_index.core.tools import QueryEngineTool

from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
from llama_index.core.agent.workflow import ReActAgent

def initialize_web_agent(llm: HuggingFaceInferenceAPI):
    hf_token = os.environ.get('HF_TOKEN')
    db = chromadb.PersistentClient(path="./chat_db")
    chroma_collection = db.get_or_create_collection("chat")
    vector_store = ChromaVectorStore(chroma_collection=chroma_collection)

    embedding_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5", device="cpu")
    index = VectorStoreIndex.from_vector_store(vector_store, embed_model=embedding_model)

    query_engine = index.as_query_engine(
        llm=llm,
        similarity_top_k=3
    )

    query_engine_tool = QueryEngineTool.from_defaults(
        query_engine=query_engine,
        name="my_query_engine",
        description="Query engine for the agent",
        return_direct=False
    )

    return ReActAgent(
        name="query_engine",
        description="Query engine for the agent",
        tools=[query_engine_tool],
        system_prompt="You are a calculator assistant. Use your tools for any math operation.",
        llm=llm
    )