Implement OpenAI chat completions in GO (#16177)

### What problem does this PR solve?

Implement OpenAI chat completions in GO

POST /api/v1/openai/<chat_id>/chat/completions

OpenAI chat cli: internal/development.md

### Type of change

- [x] Refactoring
This commit is contained in:
qinling0210
2026-06-18 18:07:27 +08:00
committed by GitHub
parent b53b5bf12c
commit 563d855780
61 changed files with 15327 additions and 2105 deletions

View File

@@ -759,10 +759,19 @@ async def async_chat(dialog, messages, stream=True, **kwargs):
yield {"answer": empty_res, "reference": kbinfos, "prompt": "\n\n### Query:\n%s" % " ".join(questions), "audio_binary": tts(tts_mdl, empty_res), "final": True}
return
kwargs["knowledge"] = "\n------\n" + "\n\n------\n\n".join(knowledges)
# Only overwrite kwargs["knowledge"] when retrieval produced something;
# otherwise preserve any caller-supplied value.
knowledge_text = "\n\n------\n\n".join(knowledges)
if knowledge_text:
kwargs["knowledge"] = "\n------\n" + knowledge_text
gen_conf = dialog.llm_setting
msg = [{"role": "system", "content": prompt_config["system"].format(**kwargs) + attachments_}]
system_content = prompt_config["system"].format(**kwargs) + attachments_
# If knowledge was retrieved but the template has no {knowledge}
# placeholder, auto-append it so the LLM still sees the context.
if knowledges and "{knowledge}" not in prompt_config.get("system", ""):
system_content += kwargs["knowledge"]
msg = [{"role": "system", "content": system_content}]
prompt4citation = ""
if knowledges and (prompt_config.get("quote", True) and kwargs.get("quote", True)):
prompt4citation = citation_prompt()
@@ -1056,7 +1065,9 @@ RULES:
- Question mentions "not null" or "excluding null"
- Add NULL check for count specific column
- DO NOT add NULL check for COUNT(*) queries (COUNT(*) counts all rows including nulls)
7. Output ONLY the SQL, no explanations"""
7. json_extract_string() returns JSON-quoted strings ("value"), so WHERE comparisons MUST wrap values in double-quotes inside single-quotes (no spaces between quotes): '"value"' (e.g. WHERE json_extract_string(chunk_data, '$.name') = '"Alice"')
8. For partial text search, use LIKE with wildcards: '"%value%"' (e.g. WHERE json_extract_string(chunk_data, '$.name') LIKE '"%Alice%"')
9. Output ONLY the SQL, no explanations"""
user_prompt = """Table: {}
Fields (EXACT case): {}
{}
@@ -1128,9 +1139,13 @@ Write SQL using exact field names above. Include doc_id, docnm_kwd for data quer
logging.debug(f"use_sql: Executing SQL retrieval (attempt {tried_times})")
tbl = settings.retriever.sql_retrieval(sql, format="json")
if tbl is None:
logging.debug("use_sql: SQL retrieval returned None")
logging.debug("use_sql: SQL retrieval failed (returned None)")
return None, sql
logging.debug(f"use_sql: SQL retrieval completed, got {len(tbl.get('rows', []))} rows")
row_count = len(tbl.get("rows", []))
if row_count == 0:
logging.debug("use_sql: SQL execution succeeded but returned 0 rows")
else:
logging.debug(f"use_sql: SQL retrieval completed, got {row_count} rows")
return tbl, sql
async def repair_table_for_missing_source_columns(previous_sql):