Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ wheels/
.DS_Store
.vscode
.idea
.codebuddy/


.venv
.env
Expand Down
1 change: 1 addition & 0 deletions config/base_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ construction:
enable_fast_mode: true
struct_weight: 0.3
max_total_communities: 100
stream: false

datasets:
hotpot:
Expand Down
1 change: 1 addition & 0 deletions config/config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ConstructionConfig:
datasets_no_chunk: list = None
chunk_size: int = 1000
overlap: int = 200
stream: bool = False

def __post_init__(self):
if self.datasets_no_chunk is None:
Expand Down
3 changes: 2 additions & 1 deletion models/constructor/kt_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(self, dataset_name, schema_path=None, mode=None, config=None):
self.llm_client = call_llm_api.LLMCompletionCall()
self.all_chunks = {}
self.mode = mode or config.construction.mode
self.stream = config.construction.stream

def load_schema(self, schema_path) -> Dict[str, Any]:
try:
Expand Down Expand Up @@ -112,7 +113,7 @@ def save_chunks_to_file(self):
logger.info(f"Chunk data saved to {chunk_file} ({len(all_data)} chunks)")

def extract_with_llm(self, prompt: str):
response = self.llm_client.call_api(prompt)
response = self.llm_client.call_api(prompt, stream=self.stream)
parsed_dict = json_repair.loads(response)
parsed_json = json.dumps(parsed_dict, ensure_ascii=False)
return parsed_json
Expand Down
28 changes: 21 additions & 7 deletions utils/call_llm_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,41 @@ def __init__(self):
else:
self.client = OpenAI(base_url=self.llm_base_url, api_key = self.llm_api_key)

def call_api(self, content: str) -> str:
def call_api(self, content: str, temperature: float = 0.3, stream: bool = False) -> str:
"""
Call API to generate text with retry mechanism.

Args:
content: Prompt content

temperature: Sampling temperature (0.0-2.0)
stream: Whether to use streaming mode

Returns:
Generated text response
"""

try:
completion = self.client.chat.completions.create(
model=self.llm_model,
messages=[{"role": "user", "content": content}],
temperature=0.3
temperature=temperature,
stream=stream
)
raw = completion.choices[0].message.content or ""

if stream:
# Handle streaming response
content_parts = []
for chunk in completion:
if chunk.choices[0].delta.content is not None:
content_parts.append(chunk.choices[0].delta.content)
raw = "".join(content_parts)
else:
# Handle non-streaming response
raw = completion.choices[0].message.content or ""

clean_completion = self._clean_llm_content(raw)
return clean_completion

except Exception as e:
logger.error(f"LLM api calling failed. Error: {e}")
raise e
Expand Down