Spaces:
Runtime error
Runtime error
File size: 35,947 Bytes
e331e72 |
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 |
from dotenv import load_dotenv
import os
import asyncio
import tempfile
from collections import deque
import time
import uuid
import json
import re
import pandas as pd
import tiktoken
import logging
import yaml
import shutil
from fastapi import Body
from fastapi import FastAPI, HTTPException, Request, BackgroundTasks, Depends
from fastapi.responses import JSONResponse, StreamingResponse
from pydantic import BaseModel, Field
from typing import List, Optional, Dict, Any, Union
from contextlib import asynccontextmanager
from web import DuckDuckGoSearchAPIWrapper
from functools import lru_cache
import requests
import subprocess
import argparse
# GraphRAG related imports
from graphrag.query.context_builder.entity_extraction import EntityVectorStoreKey
from graphrag.query.indexer_adapters import (
read_indexer_covariates,
read_indexer_entities,
read_indexer_relationships,
read_indexer_reports,
read_indexer_text_units,
)
from graphrag.query.input.loaders.dfs import store_entity_semantic_embeddings
from graphrag.query.llm.oai.chat_openai import ChatOpenAI
from graphrag.query.llm.oai.embedding import OpenAIEmbedding
from graphrag.query.llm.oai.typing import OpenaiApiType
from graphrag.query.question_gen.local_gen import LocalQuestionGen
from graphrag.query.structured_search.local_search.mixed_context import LocalSearchMixedContext
from graphrag.query.structured_search.local_search.search import LocalSearch
from graphrag.query.structured_search.global_search.community_context import GlobalCommunityContext
from graphrag.query.structured_search.global_search.search import GlobalSearch
from graphrag.vector_stores.lancedb import LanceDBVectorStore
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# Load environment variables
load_dotenv('indexing/.env')
LLM_API_BASE = os.getenv('LLM_API_BASE', '')
LLM_MODEL = os.getenv('LLM_MODEL')
LLM_PROVIDER = os.getenv('LLM_PROVIDER', 'openai').lower()
EMBEDDINGS_API_BASE = os.getenv('EMBEDDINGS_API_BASE', '')
EMBEDDINGS_MODEL = os.getenv('EMBEDDINGS_MODEL')
EMBEDDINGS_PROVIDER = os.getenv('EMBEDDINGS_PROVIDER', 'openai').lower()
INPUT_DIR = os.getenv('INPUT_DIR', './indexing/output')
ROOT_DIR = os.getenv('ROOT_DIR', 'indexing')
PORT = int(os.getenv('API_PORT', 8012))
LANCEDB_URI = f"{INPUT_DIR}/lancedb"
COMMUNITY_REPORT_TABLE = "create_final_community_reports"
ENTITY_TABLE = "create_final_nodes"
ENTITY_EMBEDDING_TABLE = "create_final_entities"
RELATIONSHIP_TABLE = "create_final_relationships"
COVARIATE_TABLE = "create_final_covariates"
TEXT_UNIT_TABLE = "create_final_text_units"
COMMUNITY_LEVEL = 2
# Global variables for storing search engines and question generator
local_search_engine = None
global_search_engine = None
question_generator = None
# Data models
class Message(BaseModel):
role: str
content: str
class QueryOptions(BaseModel):
query_type: str
preset: Optional[str] = None
community_level: Optional[int] = None
response_type: Optional[str] = None
custom_cli_args: Optional[str] = None
selected_folder: Optional[str] = None
class ChatCompletionRequest(BaseModel):
model: str
messages: List[Message]
temperature: Optional[float] = 0.7
max_tokens: Optional[int] = None
stream: Optional[bool] = False
query_options: Optional[QueryOptions] = None
class ChatCompletionResponseChoice(BaseModel):
index: int
message: Message
finish_reason: Optional[str] = None
class Usage(BaseModel):
prompt_tokens: int
completion_tokens: int
total_tokens: int
class ChatCompletionResponse(BaseModel):
id: str = Field(default_factory=lambda: f"chatcmpl-{uuid.uuid4().hex}")
object: str = "chat.completion"
created: int = Field(default_factory=lambda: int(time.time()))
model: str
choices: List[ChatCompletionResponseChoice]
usage: Usage
system_fingerprint: Optional[str] = None
def list_output_folders():
return [f for f in os.listdir(INPUT_DIR) if os.path.isdir(os.path.join(INPUT_DIR, f))]
def list_folder_contents(folder_name):
folder_path = os.path.join(INPUT_DIR, folder_name, "artifacts")
if not os.path.exists(folder_path):
return []
return [item for item in os.listdir(folder_path) if item.endswith('.parquet')]
def normalize_api_base(api_base: str) -> str:
"""Normalize the API base URL by removing trailing slashes and /v1 or /api suffixes."""
api_base = api_base.rstrip('/')
if api_base.endswith('/v1') or api_base.endswith('/api'):
api_base = api_base[:-3]
return api_base
def get_models_endpoint(api_base: str, api_type: str) -> str:
"""Get the appropriate models endpoint based on the API type."""
normalized_base = normalize_api_base(api_base)
if api_type.lower() == 'openai':
return f"{normalized_base}/v1/models"
elif api_type.lower() == 'azure':
return f"{normalized_base}/openai/deployments?api-version=2022-12-01"
else: # For other API types (e.g., local LLMs)
return f"{normalized_base}/models"
async def fetch_available_models(settings: Dict[str, Any]) -> List[str]:
"""Fetch available models from the API."""
api_base = settings['api_base']
api_type = settings['api_type']
api_key = settings['api_key']
models_endpoint = get_models_endpoint(api_base, api_type)
headers = {"Authorization": f"Bearer {api_key}"} if api_key else {}
try:
response = requests.get(models_endpoint, headers=headers, timeout=10)
response.raise_for_status()
data = response.json()
if api_type.lower() == 'openai':
return [model['id'] for model in data['data']]
elif api_type.lower() == 'azure':
return [model['id'] for model in data['value']]
else:
# Adjust this based on the actual response format of your local LLM API
return [model['name'] for model in data['models']]
except requests.exceptions.RequestException as e:
logger.error(f"Error fetching models: {str(e)}")
return []
def load_settings():
config_path = os.getenv('GRAPHRAG_CONFIG', 'config.yaml')
if os.path.exists(config_path):
with open(config_path, 'r') as config_file:
config = yaml.safe_load(config_file)
else:
config = {}
settings = {
'llm_model': os.getenv('LLM_MODEL', config.get('llm_model')),
'embedding_model': os.getenv('EMBEDDINGS_MODEL', config.get('embedding_model')),
'community_level': int(os.getenv('COMMUNITY_LEVEL', config.get('community_level', 2))),
'token_limit': int(os.getenv('TOKEN_LIMIT', config.get('token_limit', 4096))),
'api_key': os.getenv('GRAPHRAG_API_KEY', config.get('api_key')),
'api_base': os.getenv('LLM_API_BASE', config.get('api_base')),
'embeddings_api_base': os.getenv('EMBEDDINGS_API_BASE', config.get('embeddings_api_base')),
'api_type': os.getenv('API_TYPE', config.get('api_type', 'openai')),
}
return settings
return settings
async def setup_llm_and_embedder(settings):
logger.info("Setting up LLM and embedder")
try:
llm = ChatOpenAI(
api_key=settings['api_key'],
api_base=f"{settings['api_base']}/v1",
model=settings['llm_model'],
api_type=OpenaiApiType[settings['api_type'].capitalize()],
max_retries=20,
)
token_encoder = tiktoken.get_encoding("cl100k_base")
text_embedder = OpenAIEmbedding(
api_key=settings['api_key'],
api_base=f"{settings['embeddings_api_base']}/v1",
api_type=OpenaiApiType[settings['api_type'].capitalize()],
model=settings['embedding_model'],
deployment_name=settings['embedding_model'],
max_retries=20,
)
logger.info("LLM and embedder setup complete")
return llm, token_encoder, text_embedder
except Exception as e:
logger.error(f"Error setting up LLM and embedder: {str(e)}")
raise HTTPException(status_code=500, detail=f"Failed to set up LLM and embedder: {str(e)}")
async def load_context(selected_folder, settings):
"""
Load context data including entities, relationships, reports, text units, and covariates
"""
logger.info("Loading context data")
try:
input_dir = os.path.join(INPUT_DIR, selected_folder, "artifacts")
entity_df = pd.read_parquet(f"{input_dir}/{ENTITY_TABLE}.parquet")
entity_embedding_df = pd.read_parquet(f"{input_dir}/{ENTITY_EMBEDDING_TABLE}.parquet")
entities = read_indexer_entities(entity_df, entity_embedding_df, settings['community_level'])
description_embedding_store = LanceDBVectorStore(collection_name="entity_description_embeddings")
description_embedding_store.connect(db_uri=LANCEDB_URI)
store_entity_semantic_embeddings(entities=entities, vectorstore=description_embedding_store)
relationship_df = pd.read_parquet(f"{input_dir}/{RELATIONSHIP_TABLE}.parquet")
relationships = read_indexer_relationships(relationship_df)
report_df = pd.read_parquet(f"{input_dir}/{COMMUNITY_REPORT_TABLE}.parquet")
reports = read_indexer_reports(report_df, entity_df, COMMUNITY_LEVEL)
text_unit_df = pd.read_parquet(f"{input_dir}/{TEXT_UNIT_TABLE}.parquet")
text_units = read_indexer_text_units(text_unit_df)
covariate_df = pd.read_parquet(f"{input_dir}/{COVARIATE_TABLE}.parquet")
claims = read_indexer_covariates(covariate_df)
logger.info(f"Number of claim records: {len(claims)}")
covariates = {"claims": claims}
logger.info("Context data loading complete")
return entities, relationships, reports, text_units, description_embedding_store, covariates
except Exception as e:
logger.error(f"Error loading context data: {str(e)}")
raise
async def setup_search_engines(llm, token_encoder, text_embedder, entities, relationships, reports, text_units,
description_embedding_store, covariates):
"""
Set up local and global search engines
"""
logger.info("Setting up search engines")
# Set up local search engine
local_context_builder = LocalSearchMixedContext(
community_reports=reports,
text_units=text_units,
entities=entities,
relationships=relationships,
covariates=covariates,
entity_text_embeddings=description_embedding_store,
embedding_vectorstore_key=EntityVectorStoreKey.ID,
text_embedder=text_embedder,
token_encoder=token_encoder,
)
local_context_params = {
"text_unit_prop": 0.5,
"community_prop": 0.1,
"conversation_history_max_turns": 5,
"conversation_history_user_turns_only": True,
"top_k_mapped_entities": 10,
"top_k_relationships": 10,
"include_entity_rank": True,
"include_relationship_weight": True,
"include_community_rank": False,
"return_candidate_context": False,
"embedding_vectorstore_key": EntityVectorStoreKey.ID,
"max_tokens": 12_000,
}
local_llm_params = {
"max_tokens": 2_000,
"temperature": 0.0,
}
local_search_engine = LocalSearch(
llm=llm,
context_builder=local_context_builder,
token_encoder=token_encoder,
llm_params=local_llm_params,
context_builder_params=local_context_params,
response_type="multiple paragraphs",
)
# Set up global search engine
global_context_builder = GlobalCommunityContext(
community_reports=reports,
entities=entities,
token_encoder=token_encoder,
)
global_context_builder_params = {
"use_community_summary": False,
"shuffle_data": True,
"include_community_rank": True,
"min_community_rank": 0,
"community_rank_name": "rank",
"include_community_weight": True,
"community_weight_name": "occurrence weight",
"normalize_community_weight": True,
"max_tokens": 12_000,
"context_name": "Reports",
}
map_llm_params = {
"max_tokens": 1000,
"temperature": 0.0,
"response_format": {"type": "json_object"},
}
reduce_llm_params = {
"max_tokens": 2000,
"temperature": 0.0,
}
global_search_engine = GlobalSearch(
llm=llm,
context_builder=global_context_builder,
token_encoder=token_encoder,
max_data_tokens=12_000,
map_llm_params=map_llm_params,
reduce_llm_params=reduce_llm_params,
allow_general_knowledge=False,
json_mode=True,
context_builder_params=global_context_builder_params,
concurrent_coroutines=32,
response_type="multiple paragraphs",
)
logger.info("Search engines setup complete")
return local_search_engine, global_search_engine, local_context_builder, local_llm_params, local_context_params
def format_response(response):
"""
Format the response by adding appropriate line breaks and paragraph separations.
"""
paragraphs = re.split(r'\n{2,}', response)
formatted_paragraphs = []
for para in paragraphs:
if '```' in para:
parts = para.split('```')
for i, part in enumerate(parts):
if i % 2 == 1: # This is a code block
parts[i] = f"\n```\n{part.strip()}\n```\n"
para = ''.join(parts)
else:
para = para.replace('. ', '.\n')
formatted_paragraphs.append(para.strip())
return '\n\n'.join(formatted_paragraphs)
@asynccontextmanager
async def lifespan(app: FastAPI):
global settings
try:
logger.info("Loading settings...")
settings = load_settings()
logger.info("Settings loaded successfully.")
except Exception as e:
logger.error(f"Error loading settings: {str(e)}")
raise
yield
logger.info("Shutting down...")
app = FastAPI(lifespan=lifespan)
# Create a cache for loaded contexts
context_cache = {}
@lru_cache()
def get_settings():
return load_settings()
async def get_context(selected_folder: str, settings: dict = Depends(get_settings)):
if selected_folder not in context_cache:
try:
llm, token_encoder, text_embedder = await setup_llm_and_embedder(settings)
entities, relationships, reports, text_units, description_embedding_store, covariates = await load_context(selected_folder, settings)
local_search_engine, global_search_engine, local_context_builder, local_llm_params, local_context_params = await setup_search_engines(
llm, token_encoder, text_embedder, entities, relationships, reports, text_units,
description_embedding_store, covariates
)
question_generator = LocalQuestionGen(
llm=llm,
context_builder=local_context_builder,
token_encoder=token_encoder,
llm_params=local_llm_params,
context_builder_params=local_context_params,
)
context_cache[selected_folder] = {
"local_search_engine": local_search_engine,
"global_search_engine": global_search_engine,
"question_generator": question_generator
}
except Exception as e:
logger.error(f"Error loading context for folder {selected_folder}: {str(e)}")
raise HTTPException(status_code=500, detail=f"Failed to load context for folder {selected_folder}")
return context_cache[selected_folder]
@app.post("/v1/chat/completions")
async def chat_completions(request: ChatCompletionRequest):
try:
logger.info(f"Received request for model: {request.model}")
if request.model == "direct-chat":
logger.info("Routing to direct chat")
return await run_direct_chat(request)
elif request.model.startswith("graphrag-"):
logger.info("Routing to GraphRAG query")
if not request.query_options or not request.query_options.selected_folder:
raise HTTPException(status_code=400, detail="Selected folder is required for GraphRAG queries")
return await run_graphrag_query(request)
elif request.model == "duckduckgo-search:latest":
logger.info("Routing to DuckDuckGo search")
return await run_duckduckgo_search(request)
elif request.model == "full-model:latest":
logger.info("Routing to full model search")
return await run_full_model_search(request)
else:
raise HTTPException(status_code=400, detail=f"Invalid model specified: {request.model}")
except HTTPException as he:
logger.error(f"HTTP Exception: {str(he)}")
raise he
except Exception as e:
logger.error(f"Error in chat completion: {str(e)}", exc_info=True)
raise HTTPException(status_code=500, detail=str(e))
async def run_direct_chat(request: ChatCompletionRequest) -> ChatCompletionResponse:
try:
if not LLM_API_BASE:
raise ValueError("LLM_API_BASE environment variable is not set")
headers = {"Content-Type": "application/json"}
payload = {
"model": LLM_MODEL,
"messages": [{"role": msg.role, "content": msg.content} for msg in request.messages],
"stream": False
}
# Optional parameters
if request.temperature is not None:
payload["temperature"] = request.temperature
if request.max_tokens is not None:
payload["max_tokens"] = request.max_tokens
full_url = f"{normalize_api_base(LLM_API_BASE)}/v1/chat/completions"
logger.info(f"Sending request to: {full_url}")
logger.info(f"Payload: {payload}")
try:
response = requests.post(full_url, json=payload, headers=headers, timeout=10)
response.raise_for_status()
except requests.exceptions.RequestException as req_ex:
logger.error(f"Request to LLM API failed: {str(req_ex)}")
if isinstance(req_ex, requests.exceptions.ConnectionError):
raise HTTPException(status_code=503, detail="Unable to connect to LLM API. Please check your API settings.")
elif isinstance(req_ex, requests.exceptions.Timeout):
raise HTTPException(status_code=504, detail="Request to LLM API timed out")
else:
raise HTTPException(status_code=500, detail=f"Request to LLM API failed: {str(req_ex)}")
result = response.json()
logger.info(f"Received response: {result}")
content = result['choices'][0]['message']['content']
return ChatCompletionResponse(
model=LLM_MODEL,
choices=[
ChatCompletionResponseChoice(
index=0,
message=Message(
role="assistant",
content=content
),
finish_reason=None
)
],
usage=None
)
except HTTPException as he:
logger.error(f"HTTP Exception in direct chat: {str(he)}")
raise he
except Exception as e:
logger.error(f"Unexpected error in direct chat: {str(e)}")
raise HTTPException(status_code=500, detail=f"An unexpected error occurred during the direct chat: {str(e)}")
def get_embeddings(text: str) -> List[float]:
settings = load_settings()
embeddings_api_base = settings['embeddings_api_base']
headers = {"Content-Type": "application/json"}
if EMBEDDINGS_PROVIDER == 'ollama':
payload = {
"model": EMBEDDINGS_MODEL,
"prompt": text
}
full_url = f"{embeddings_api_base}/api/embeddings"
else: # OpenAI-compatible API
payload = {
"model": EMBEDDINGS_MODEL,
"input": text
}
full_url = f"{embeddings_api_base}/v1/embeddings"
try:
response = requests.post(full_url, json=payload, headers=headers)
response.raise_for_status()
except requests.exceptions.RequestException as req_ex:
logger.error(f"Request to Embeddings API failed: {str(req_ex)}")
raise HTTPException(status_code=500, detail=f"Failed to get embeddings: {str(req_ex)}")
result = response.json()
if EMBEDDINGS_PROVIDER == 'ollama':
return result['embedding']
else:
return result['data'][0]['embedding']
async def run_graphrag_query(request: ChatCompletionRequest) -> ChatCompletionResponse:
try:
query_options = request.query_options
query = request.messages[-1].content # Get the last user message as the query
cmd = ["python", "-m", "graphrag.query"]
cmd.extend(["--data", f"./indexing/output/{query_options.selected_folder}/artifacts"])
cmd.extend(["--method", query_options.query_type.split('-')[1]]) # 'global' or 'local'
if query_options.community_level:
cmd.extend(["--community_level", str(query_options.community_level)])
if query_options.response_type:
cmd.extend(["--response_type", query_options.response_type])
# Handle preset CLI args
if query_options.preset and query_options.preset != "Custom Query":
preset_args = get_preset_args(query_options.preset)
cmd.extend(preset_args)
# Handle custom CLI args
if query_options.custom_cli_args:
cmd.extend(query_options.custom_cli_args.split())
cmd.append(query)
logger.info(f"Executing GraphRAG query: {' '.join(cmd)}")
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
raise Exception(f"GraphRAG query failed: {result.stderr}")
return ChatCompletionResponse(
model=request.model,
choices=[
ChatCompletionResponseChoice(
index=0,
message=Message(
role="assistant",
content=result.stdout
),
finish_reason="stop"
)
],
usage=Usage(
prompt_tokens=0,
completion_tokens=0,
total_tokens=0
)
)
except Exception as e:
logger.error(f"Error in GraphRAG query: {str(e)}")
raise HTTPException(status_code=500, detail=f"An error occurred during the GraphRAG query: {str(e)}")
def get_preset_args(preset: str) -> List[str]:
preset_args = {
"Default Global Search": ["--community_level", "2", "--response_type", "Multiple Paragraphs"],
"Default Local Search": ["--community_level", "2", "--response_type", "Multiple Paragraphs"],
"Detailed Global Analysis": ["--community_level", "3", "--response_type", "Multi-Page Report"],
"Detailed Local Analysis": ["--community_level", "3", "--response_type", "Multi-Page Report"],
"Quick Global Summary": ["--community_level", "1", "--response_type", "Single Paragraph"],
"Quick Local Summary": ["--community_level", "1", "--response_type", "Single Paragraph"],
"Global Bullet Points": ["--community_level", "2", "--response_type", "List of 3-7 Points"],
"Local Bullet Points": ["--community_level", "2", "--response_type", "List of 3-7 Points"],
"Comprehensive Global Report": ["--community_level", "4", "--response_type", "Multi-Page Report"],
"Comprehensive Local Report": ["--community_level", "4", "--response_type", "Multi-Page Report"],
"High-Level Global Overview": ["--community_level", "1", "--response_type", "Single Page"],
"High-Level Local Overview": ["--community_level", "1", "--response_type", "Single Page"],
"Focused Global Insight": ["--community_level", "3", "--response_type", "Single Paragraph"],
"Focused Local Insight": ["--community_level", "3", "--response_type", "Single Paragraph"],
}
return preset_args.get(preset, [])
ddg_search = DuckDuckGoSearchAPIWrapper(max_results=5)
async def run_duckduckgo_search(request: ChatCompletionRequest) -> ChatCompletionResponse:
query = request.messages[-1].content
results = ddg_search.results(query, max_results=5)
if not results:
content = "No results found for the given query."
else:
content = "DuckDuckGo Search Results:\n\n"
for result in results:
content += f"Title: {result['title']}\n"
content += f"Snippet: {result['snippet']}\n"
content += f"Link: {result['link']}\n"
if 'date' in result:
content += f"Date: {result['date']}\n"
if 'source' in result:
content += f"Source: {result['source']}\n"
content += "\n"
return ChatCompletionResponse(
model=request.model,
choices=[
ChatCompletionResponseChoice(
index=0,
message=Message(
role="assistant",
content=content
),
finish_reason="stop"
)
],
usage=Usage(
prompt_tokens=0,
completion_tokens=0,
total_tokens=0
)
)
async def run_full_model_search(request: ChatCompletionRequest) -> ChatCompletionResponse:
query = request.messages[-1].content
# Run all search types
graphrag_global = await run_graphrag_query(ChatCompletionRequest(model="graphrag-global-search:latest", messages=request.messages, query_options=request.query_options))
graphrag_local = await run_graphrag_query(ChatCompletionRequest(model="graphrag-local-search:latest", messages=request.messages, query_options=request.query_options))
duckduckgo = await run_duckduckgo_search(request)
# Combine results
combined_content = f"""Full Model Search Results:
Global Search:
{graphrag_global.choices[0].message.content}
Local Search:
{graphrag_local.choices[0].message.content}
DuckDuckGo Search:
{duckduckgo.choices[0].message.content}
"""
return ChatCompletionResponse(
model=request.model,
choices=[
ChatCompletionResponseChoice(
index=0,
message=Message(
role="assistant",
content=combined_content
),
finish_reason="stop"
)
],
usage=Usage(
prompt_tokens=0,
completion_tokens=0,
total_tokens=0
)
)
@app.get("/health")
async def health_check():
return {"status": "ok"}
@app.get("/v1/models")
async def list_models():
settings = load_settings()
try:
api_models = await fetch_available_models(settings)
except Exception as e:
logger.error(f"Error fetching API models: {str(e)}")
api_models = []
# Include the hardcoded models
hardcoded_models = [
{"id": "graphrag-local-search:latest", "object": "model", "owned_by": "graphrag"},
{"id": "graphrag-global-search:latest", "object": "model", "owned_by": "graphrag"},
{"id": "duckduckgo-search:latest", "object": "model", "owned_by": "duckduckgo"},
{"id": "full-model:latest", "object": "model", "owned_by": "combined"},
]
# Combine API models with hardcoded models
all_models = [{"id": model, "object": "model", "owned_by": "api"} for model in api_models] + hardcoded_models
return JSONResponse(content={"data": all_models})
class PromptTuneRequest(BaseModel):
root: str = "./{ROOT_DIR}"
domain: Optional[str] = None
method: str = "random"
limit: int = 15
language: Optional[str] = None
max_tokens: int = 2000
chunk_size: int = 200
no_entity_types: bool = False
output: str = "./{ROOT_DIR}/prompts"
class PromptTuneResponse(BaseModel):
status: str
message: str
# Global variable to store the latest logs
prompt_tune_logs = deque(maxlen=100)
async def run_prompt_tuning(request: PromptTuneRequest):
cmd = ["python", "-m", "graphrag.prompt_tune"]
# Create a temporary directory for output
with tempfile.TemporaryDirectory() as temp_output:
# Expand environment variables in the root path
root_path = os.path.expandvars(request.root)
cmd.extend(["--root", root_path])
cmd.extend(["--method", request.method])
cmd.extend(["--limit", str(request.limit)])
if request.domain:
cmd.extend(["--domain", request.domain])
if request.language:
cmd.extend(["--language", request.language])
cmd.extend(["--max-tokens", str(request.max_tokens)])
cmd.extend(["--chunk-size", str(request.chunk_size)])
if request.no_entity_types:
cmd.append("--no-entity-types")
# Use the temporary directory for output
cmd.extend(["--output", temp_output])
logger.info(f"Executing prompt tuning command: {' '.join(cmd)}")
try:
process = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
async def read_stream(stream):
while True:
line = await stream.readline()
if not line:
break
line = line.decode().strip()
prompt_tune_logs.append(line)
logger.info(line)
await asyncio.gather(
read_stream(process.stdout),
read_stream(process.stderr)
)
await process.wait()
if process.returncode == 0:
logger.info("Prompt tuning completed successfully")
# Replace the existing template files with the newly generated prompts
dest_dir = os.path.join(ROOT_DIR, "prompts")
for filename in os.listdir(temp_output):
if filename.endswith(".txt"):
source_file = os.path.join(temp_output, filename)
dest_file = os.path.join(dest_dir, filename)
shutil.move(source_file, dest_file)
logger.info(f"Replaced {filename} in {dest_file}")
return PromptTuneResponse(status="success", message="Prompt tuning completed successfully. Existing prompts have been replaced.")
else:
logger.error("Prompt tuning failed")
return PromptTuneResponse(status="error", message="Prompt tuning failed. Check logs for details.")
except Exception as e:
logger.error(f"Prompt tuning failed: {str(e)}")
return PromptTuneResponse(status="error", message=f"Prompt tuning failed: {str(e)}")
@app.post("/v1/prompt_tune")
async def prompt_tune(request: PromptTuneRequest, background_tasks: BackgroundTasks):
background_tasks.add_task(run_prompt_tuning, request)
return {"status": "started", "message": "Prompt tuning process has been started in the background"}
@app.get("/v1/prompt_tune_status")
async def prompt_tune_status():
return {
"status": "running" if prompt_tune_logs else "idle",
"logs": list(prompt_tune_logs)
}
class IndexingRequest(BaseModel):
llm_model: str
embed_model: str
llm_api_base: str
embed_api_base: str
root: str
verbose: bool = False
nocache: bool = False
resume: Optional[str] = None
reporter: str = "rich"
emit: List[str] = ["parquet"]
custom_args: Optional[str] = None
llm_params: Dict[str, Any] = Field(default_factory=dict)
embed_params: Dict[str, Any] = Field(default_factory=dict)
# Global variable to store the latest indexing logs
indexing_logs = deque(maxlen=100)
async def run_indexing(request: IndexingRequest):
cmd = ["python", "-m", "graphrag.index"]
cmd.extend(["--root", request.root])
if request.verbose:
cmd.append("--verbose")
if request.nocache:
cmd.append("--nocache")
if request.resume:
cmd.extend(["--resume", request.resume])
cmd.extend(["--reporter", request.reporter])
cmd.extend(["--emit", ",".join(request.emit)])
# Set environment variables for LLM and embedding models
env: Dict[str, Any] = os.environ.copy()
env["GRAPHRAG_LLM_MODEL"] = request.llm_model
env["GRAPHRAG_EMBED_MODEL"] = request.embed_model
env["GRAPHRAG_LLM_API_BASE"] = LLM_API_BASE
env["GRAPHRAG_EMBED_API_BASE"] = EMBEDDINGS_API_BASE
# Set environment variables for LLM parameters
for key, value in request.llm_params.items():
env[f"GRAPHRAG_LLM_{key.upper()}"] = str(value)
# Set environment variables for embedding parameters
for key, value in request.embed_params.items():
env[f"GRAPHRAG_EMBED_{key.upper()}"] = str(value)
# Add custom CLI arguments
if request.custom_args:
cmd.extend(request.custom_args.split())
logger.info(f"Executing indexing command: {' '.join(cmd)}")
logger.info(f"Environment variables: {env}")
try:
process = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
env=env
)
async def read_stream(stream):
while True:
line = await stream.readline()
if not line:
break
line = line.decode().strip()
indexing_logs.append(line)
logger.info(line)
await asyncio.gather(
read_stream(process.stdout),
read_stream(process.stderr)
)
await process.wait()
if process.returncode == 0:
logger.info("Indexing completed successfully")
return {"status": "success", "message": "Indexing completed successfully"}
else:
logger.error("Indexing failed")
return {"status": "error", "message": "Indexing failed. Check logs for details."}
except Exception as e:
logger.error(f"Indexing failed: {str(e)}")
return {"status": "error", "message": f"Indexing failed: {str(e)}"}
@app.post("/v1/index")
async def start_indexing(request: IndexingRequest, background_tasks: BackgroundTasks):
background_tasks.add_task(run_indexing, request)
return {"status": "started", "message": "Indexing process has been started in the background"}
@app.get("/v1/index_status")
async def indexing_status():
return {
"status": "running" if indexing_logs else "idle",
"logs": list(indexing_logs)
}
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Launch the GraphRAG API server")
parser.add_argument("--host", type=str, default="127.0.0.1", help="Host to bind the server to")
parser.add_argument("--port", type=int, default=PORT, help="Port to bind the server to")
parser.add_argument("--reload", action="store_true", help="Enable auto-reload mode")
args = parser.parse_args()
import uvicorn
uvicorn.run(
"api:app",
host=args.host,
port=args.port,
reload=args.reload
)
|