Skip to content

armgit / LEANN

Public Imported from GitHub
0

[MLsys2026 Best Paper]: https://arxiv.org/abs/2506.08276. RAG on Everything with LEANN. Enjoy 97% storage savings while running a fast, accurate, and 100% private RAG application on your personal device.

Resolve merge conflict: add serve_api method alongside react_agent

aakash7e9e3a0d4f52
diff --git a/packages/leann-core/src/leann/cli.py b/packages/leann-core/src/leann/cli.py
index f63b821..19bd6e5 100644
--- a/packages/leann-core/src/leann/cli.py
+++ b/packages/leann-core/src/leann/cli.py
@@ -1831,6 +1831,35 @@ Examples:
f" {entry['iteration']}. {entry['action']} ({entry['results_count']} results)"
)
+ async def serve_api(self, args):
+ """Start the HTTP API server."""
+ import os
+
+ try:
+ from .server import main as server_main
+
+ # Override host/port if provided via CLI args
+ if args.host:
+ os.environ["LEANN_SERVER_HOST"] = args.host
+ if args.port:
+ os.environ["LEANN_SERVER_PORT"] = str(args.port)
+
+ # Run the server (this is blocking, so we don't await it)
+ # The server_main function handles uvicorn.run which blocks
+ server_main()
+ except ImportError as e:
+ print(
+ "❌ HTTP server dependencies not installed.\n"
+ "Install them with:\n"
+ " uv pip install 'leann-core[server]'\n"
+ "or:\n"
+ " uv pip install 'fastapi>=0.115' 'pydantic>=2' 'uvicorn[standard]'\n"
+ )
+ raise SystemExit(1) from e
+ except Exception as e:
+ print(f"❌ Error starting server: {e}")
+ raise SystemExit(1) from e
+
async def run(self, args=None):
parser = self.create_parser()
@@ -1861,6 +1890,8 @@ Examples:
elif args.command == "react":
with suppress_cpp_output(suppress):
await self.react_agent(args)
+ elif args.command == "serve":
+ await self.serve_api(args)
else:
parser.print_help()
diff --git a/packages/leann-core/src/leann/react_agent.py b/packages/leann-core/src/leann/react_agent.py
index 1a63876..f05c87c 100644
--- a/packages/leann-core/src/leann/react_agent.py
+++ b/packages/leann-core/src/leann/react_agent.py
@@ -62,7 +62,9 @@ class ReActAgent:
return "No results found."
formatted = []
for i, result in enumerate(results, 1):
- formatted.append(f"[Result {i}] (Score: {result.score:.3f})\n{result.text[:500]}...")
+ formatted.append(
+ f"[Result {i}] (Score: {result.score:.3f})\n{result.text[:500]}..."
+ )
if result.metadata.get("source"):
formatted[-1] += f"\nSource: {result.metadata['source']}"
return "\n\n".join(formatted)
@@ -218,7 +220,7 @@ Action: search("query") OR Final Answer: [your answer]
return final_answer
# Perform search action
- logger.info(f'🔍 Action: search("{action}")')
+ logger.info(f"🔍 Action: search(\"{action}\")")
results = self.search(action, top_k=top_k)
# Format observation
@@ -286,3 +288,4 @@ def create_react_agent(
"""
searcher = LeannSearcher(index_path, **searcher_kwargs)
return ReActAgent(searcher=searcher, llm_config=llm_config, max_iterations=max_iterations)
+