ChuanhuChatGPT is an advanced GUI for interacting with the ChatGPT API and various large language models (LLMs). Among its numerous features, it includes robust functionalities for saving and managing chat history and embedding indices. This guide will provide an overview of how to set up and utilize these features effectively.

1. Setting Up the Setup Wizard

The setup_wizard function initializes the setup process if the config.json file is not found. This function configures several key settings, including local embeddings and methods for naming conversation histories.

1.1. Path to setup_wizard:

  • File: modules/utils.py
  • Location: Defined starting at line 1059
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
import os

def setup_wizard():
if not os.path.exists("config.json"):
wizard = SetupWizard()
flag = False
wizard["multi_api_key"] = True

# Setting local_embedding
wizard.set(
[
ConfigItem("local_embedding", "本地编制索引", type=ConfigType.Bool),
"是否在本地编制知识库索引?如果是,可以在使用本地模型时离线使用知识库,否则使用OpenAI服务来编制索引(需要OpenAI API Key)。请确保你的电脑有至少16GB内存。"
]
)

print(
colorama.Back.GREEN + i18n("现在开始设置其他在线模型的API Key") + colorama.Style.RESET_ALL
)

# Auto naming chat history
wizard.set(
[
ConfigItem(
"chat_name_method_index",
"自动命名对话历史的方式(0: 使用日期时间命名, 1: 使用第一条提问命名, 2: 使用模型自动总结。)",
type=ConfigType.Number,
default=2,
)
],
"是否选择自动命名对话历史的方式?"
)

2. Managing Chat History: Exporting to JSON and Markdown

2.1. Export to JSON

To save chat history in JSON format, the functionality is implemented within ChuanhuChatbot.py, handling session states and user interactions.

1
2
3
4
5
import json

def save_history_to_json(chat_history, file_path='chat_history.json'):
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(chat_history, f, ensure_ascii=False, indent=4)

2.2. Export to Markdown

To export chat history to Markdown, you can format the conversation entries appropriately and save them as a Markdown file.

1
2
3
4
5
6
7
8
9
10
11
12
def save_history_to_markdown(chat_history, file_path='chat_history.md'):
with open(file_path, 'w', encoding='utf-8') as f:
for entry in chat_history:
f.write(f"### {entry['role']}\n\n{entry['content']}\n\n")

# Sample usage
chat_history = [
{'role': 'user', 'content': 'What is the capital of France?'},
{'role': 'assistant', 'content': 'The capital of France is Paris.'}
]

save_history_to_markdown(chat_history)

3. Handling Embedding Indices with FAISS

FAISS is used in ChuanhuChatGPT for efficient storage and retrieval of text embeddings, enabling powerful search and query capabilities.

3.1. Creating and Saving Embeddings

  • File: modules/index_func.py
  • Function: Embedding creation and saving
1
2
3
4
5
6
7
from langchain.vectorstores import FAISS
from langchain.embeddings.openai import OpenAIEmbeddings

def create_and_save_index(documents, index_path='faiss.index'):
embeddings = OpenAIEmbeddings()
index = FAISS.from_documents(documents, embeddings)
index.save(index_path)

3.2. Loading the Index

Load the saved FAISS index for efficient querying of stored embeddings.

1
2
def load_index(index_path='faiss.index'):
return FAISS.load(index_path)

4. Conclusion

ChuanhuChatGPT offers extensive capabilities for managing chat history and embedding indices. By leveraging these features, you can ensure efficient storage, retrieval, and documentation of your interactions. For detailed implementation and updates, refer to the ChuanhuChatGPT repository on GitHub【21†source】【22†source】【23†source】【24†source】【25†source】.

These features enhance the usability and flexibility of ChuanhuChatGPT, making it a robust tool for interacting with and managing large language models.


🍀后记🍀
博客的关键词集中在编程、算法、机器人、人工智能、数学等等,持续高质量输出中。
🌸唠嗑QQ群兔叽の魔术工房 (942848525)
⭐B站账号白拾Official(活跃于知识区和动画区)
✨GitHub主页YangSierCode000(工程文件)
⛳Discord社区AierLab(人工智能社区)