working on retrieve
This commit is contained in:
parent
e4aa30e8c1
commit
1a77b84921
|
|
@ -13,6 +13,7 @@ services:
|
|||
LANGFUSE_HOST: ${LANGFUSE_HOST}
|
||||
LANGFUSE_PUBLIC_KEY: ${LANGFUSE_PUBLIC_KEY}
|
||||
LANGFUSE_SECRET_KEY: ${LANGFUSE_SECRET_KEY}
|
||||
ELASTICSEARCH_INDEX: ${ELASTICSEARCH_INDEX}
|
||||
|
||||
extra_hosts:
|
||||
- "host.docker.internal:host-gateway"
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ from langchain_core.prompts import ChatPromptTemplate
|
|||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger("brunix-engine")
|
||||
|
||||
|
||||
class BrunixEngine(brunix_pb2_grpc.AssistanceEngineServicer):
|
||||
def __init__(self):
|
||||
|
||||
|
|
@ -24,25 +25,47 @@ class BrunixEngine(brunix_pb2_grpc.AssistanceEngineServicer):
|
|||
|
||||
self.llm = Ollama(base_url=self.base_url, model=self.model_name)
|
||||
|
||||
self.embeddings = OllamaEmbeddings(base_url=self.base_url, model="nomic-embed-text")
|
||||
self.embeddings = OllamaEmbeddings(
|
||||
base_url=self.base_url, model="qwen3-embedding:0.6b"
|
||||
)
|
||||
|
||||
es_url = os.getenv("ELASTICSEARCH_URL", "http://elasticsearch:9200")
|
||||
logger.info(f"ElasticSearch on: {es_url}")
|
||||
|
||||
self.vector_store = ElasticsearchStore(
|
||||
es_url=es_url,
|
||||
index_name="avap_manuals",
|
||||
embedding=self.embeddings
|
||||
index_name=os.getenv("ELASTICSEARCH_INDEX"),
|
||||
embedding=self.embeddings,
|
||||
query_field="text",
|
||||
vector_query_field="embedding",
|
||||
)
|
||||
|
||||
def format_context(docs) -> str:
|
||||
parts = []
|
||||
for i, d in enumerate(docs, start=1):
|
||||
meta = d.metadata or {}
|
||||
source = meta.get("source", "unknown")
|
||||
doc_id = meta.get("doc_id", "unknown")
|
||||
chunk_id = meta.get("chunk_id", "unknown")
|
||||
|
||||
parts.append(
|
||||
f"[{i}] source={source} doc_id={doc_id} chunk_id={chunk_id}\n{d.page_content}"
|
||||
)
|
||||
return "\n\n---\n\n".join(parts)
|
||||
|
||||
def AskAgent(self, request, context):
|
||||
logger.info(f"request {request.session_id}): {request.query[:50]}.")
|
||||
|
||||
docs_and_scores = self.vector_store.similarity_search_with_score(
|
||||
request.query, k=4
|
||||
)
|
||||
|
||||
try:
|
||||
context_text = "AVAP is a virtual programming language for API development."
|
||||
context_text = self.format_context([doc for doc, _ in docs_and_scores])
|
||||
# 4. Prompt Engineering
|
||||
prompt = ChatPromptTemplate.from_template("""
|
||||
You are Brunix, the 101OBEX artificial intelligence for the AVAP Sphere platform. Respond in a professional manner.
|
||||
You are a helpful assistant. Use the following retrieved documents to answer the question.
|
||||
If you don't know the answer, say you don't know.
|
||||
|
||||
CONTEXT:
|
||||
{context}
|
||||
|
|
@ -53,18 +76,21 @@ class BrunixEngine(brunix_pb2_grpc.AssistanceEngineServicer):
|
|||
|
||||
chain = prompt | self.llm
|
||||
|
||||
for chunk in chain.stream({"context": context_text, "question": request.query}):
|
||||
for chunk in chain.stream(
|
||||
{"context": context_text, "question": request.query}
|
||||
):
|
||||
yield brunix_pb2.AgentResponse(
|
||||
text=str(chunk),
|
||||
avap_code="AVAP-2026",
|
||||
is_final=False
|
||||
text=str(chunk), avap_code="AVAP-2026", is_final=False
|
||||
)
|
||||
|
||||
yield brunix_pb2.AgentResponse(text="", avap_code="", is_final=True)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error in AskAgent: {str(e)}")
|
||||
yield brunix_pb2.AgentResponse(text=f"[Error Motor]: {str(e)}", is_final=True)
|
||||
yield brunix_pb2.AgentResponse(
|
||||
text=f"[Error Motor]: {str(e)}", is_final=True
|
||||
)
|
||||
|
||||
|
||||
def serve():
|
||||
|
||||
|
|
@ -73,15 +99,16 @@ def serve():
|
|||
brunix_pb2_grpc.add_AssistanceEngineServicer_to_server(BrunixEngine(), server)
|
||||
|
||||
SERVICE_NAMES = (
|
||||
brunix_pb2.DESCRIPTOR.services_by_name['AssistanceEngine'].full_name,
|
||||
brunix_pb2.DESCRIPTOR.services_by_name["AssistanceEngine"].full_name,
|
||||
reflection.SERVICE_NAME,
|
||||
)
|
||||
reflection.enable_server_reflection(SERVICE_NAMES, server)
|
||||
|
||||
server.add_insecure_port('[::]:50051')
|
||||
server.add_insecure_port("[::]:50051")
|
||||
logger.info("Brunix Engine on port 50051")
|
||||
server.start()
|
||||
server.wait_for_termination()
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
if __name__ == "__main__":
|
||||
serve()
|
||||
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue