MCPcopy Create free account
hub / github.com/dataease/SQLBot / get_last_conversation_rounds

Function get_last_conversation_rounds

backend/apps/chat/task/llm.py:1876–1899  ·  view source on GitHub ↗

获取最后N轮对话,处理不完整对话的情况

(messages, rounds=settings.GENERATE_SQL_QUERY_HISTORY_ROUND_COUNT)

Source from the content-addressed store, hash-verified

1874
1875
1876def get_last_conversation_rounds(messages, rounds=settings.GENERATE_SQL_QUERY_HISTORY_ROUND_COUNT):
1877 """获取最后N轮对话,处理不完整对话的情况"""
1878 if not messages or rounds <= 0:
1879 return []
1880
1881 # 找到所有用户消息的位置
1882 human_indices = []
1883 for index, msg in enumerate(messages):
1884 if msg.get('type') == 'human':
1885 human_indices.append(index)
1886
1887 # 如果没有用户消息,返回空
1888 if not human_indices:
1889 return []
1890
1891 # 计算从哪个索引开始
1892 if len(human_indices) <= rounds:
1893 # 如果用户消息数少于等于需要的轮数,从第一个用户消息开始
1894 start_index = human_indices[0]
1895 else:
1896 # 否则,从倒数第N个用户消息开始
1897 start_index = human_indices[-rounds]
1898
1899 return messages[start_index:]

Callers 1

init_messagesMethod · 0.85

Calls 2

getMethod · 0.65
appendMethod · 0.45

Tested by

no test coverage detected