Skip to content
Merged
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
28 changes: 27 additions & 1 deletion changai/changai/api/v2/ai_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,32 @@ def get_doctype(doc:str,docname: str):
def get_settings():
return frappe.get_single("ChangAI Settings")


_CLAUDE_CLIENT = None
_CLAUDE_API_KEY = None

def get_claude_client():
global _CLAUDE_CLIENT, _CLAUDE_API_KEY

settings = get_settings()
api_key = (getattr(settings, "claude_api_key", None) or "").strip()

if not api_key:
api_key = (os.getenv("ANTHROPIC_API_KEY") or "").strip()

if not api_key:
frappe.throw(
_("Claude API key is not configured."),
title=_("Missing Claude API Key")
)

if _CLAUDE_CLIENT is None or _CLAUDE_API_KEY != api_key:
_CLAUDE_CLIENT = Anthropic(api_key=api_key)
_CLAUDE_API_KEY = api_key

return _CLAUDE_CLIENT


@frappe.whitelist(allow_guest=False)
def translate_and_store(docname: str, doctype: str, from_field: str, to_field: str, text: str, to_language: str):
"""
Expand Down Expand Up @@ -43,7 +69,7 @@ def translate_and_store(docname: str, doctype: str, from_field: str, to_field: s
title=_("Missing Claude API Key")
)
try:
client = Anthropic(api_key=api_key)
client = get_claude_client()
prompt = f"""
Translate the following text into {to_language}.
Return ONLY the translated text.
Expand Down
1 change: 1 addition & 0 deletions changai/changai/api/v2/assets/changai_alias_map.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"hlw": "hello",
"heyy": "hey",
"heyyy": "hey",
"nce": "nice",
"heyyyy": "hey",
"hai": "hi",
"hi": "hi",
Expand Down
Binary file modified changai/changai/api/v2/assets/non_erp_combined.processed.cache.pkl
Binary file not shown.
7 changes: 7 additions & 0 deletions changai/changai/api/v2/assets/non_erp_combined.processed.json
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,13 @@
"priority": 90,
"is_active": true
},
{
"category": "conversation",
"user_input": "nice",
"response": "Thanks for the kind words! changAI is here to serve you.",
"priority": 90,
"is_active": true
},
{
"category": "conversation",
"user_input": "nice to talk to you",
Expand Down
3 changes: 1 addition & 2 deletions changai/changai/api/v2/format_output.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import random
from decimal import Decimal
from typing import Any, Dict, List, Optional, Set
import frappe
Expand Down Expand Up @@ -584,7 +583,7 @@ def format_sql_response(
"source_fields": source_fields,
}

@frappe.whitelist(allow_guest=True)
@frappe.whitelist(allow_guest=False)
def local_format(sql, sample_rows):
row_count = len(sample_rows)
result = format_sql_response(sql, row_count, sample_rows)
Expand Down
1 change: 0 additions & 1 deletion changai/changai/api/v2/schema_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ def validate_sql_schema(sql: str, dialect: str = "mysql") -> dict:
@frappe.whitelist(allow_guest=False)
def check_file_updates(file_name=None):
settings = frappe.get_single("ChangAI Settings")

if file_name == "master_data.yaml":
last_sync = settings.last_masterdata_sync
elif file_name == "schema.yaml":
Expand Down
12 changes: 10 additions & 2 deletions changai/changai/api/v2/store_chats.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,24 @@ def respond_from_cache(user_question:str):
- For vague money questions, clarify the business meaning as actual, ordered, quoted, paid, or outstanding, but do not guess the document type incorrectly.
- If the user says "spend", treat it as actual purchase/expense, not quotation or order commitment, unless the user explicitly mentions order, quotation, or planned purchase.
- Preserve all filter conditions, status values, and keywords from the original question — never drop them during rewriting.

- Do NOT add dates, filters, entities, statuses, or assumptions unless explicitly present in the user question or clearly inferred from conversation memory.
Use chat history only when the current query clearly implies continuation or follow-up context. Never assume dates, filters, entities, or conditions from previous messages unless strongly indicated.
Chat history:
{rows}
User:
{qstn}
Use only the most relevant tables and fields required for the user query.
Use only valid tables and fields from the provided schema context, regardless of retrieval ranking order. Choose fields based on business meaning and user intent, not rank position. Never invent schema elements. Always return any one clear user-readable business fields, not only technical IDs, unless explicitly requested. If the query is ambiguous, ask for clarification and set "clarify": true.
"""
USER_PROMPT = """Chat History:
{rows}

User Question:
{qstn}"""
@frappe.whitelist(allow_guest=False)
def inject_prompt(user_qstn: str, session_id: str) -> str:
rows=get_chat_history(session_id)
prompt=PROMPT_FOLLOWUP.format(rows=rows,qstn=user_qstn)
prompt=USER_PROMPT.format(rows=rows,qstn=user_qstn)
return prompt


Expand Down
Loading
Loading