ITPub博客

首页 > IT职业 > IT职场 > charset-normalizer 是一个用于可靠检测各种文本文件或数据块

charset-normalizer 是一个用于可靠检测各种文本文件或数据块

投稿 IT职场 作者:ecclw459 时间:2026-01-26 16:20:41 0 删除 编辑

# 尝试检测这个可能是乱码的字节串 unknown_bytes = b'\xc3\xa9l\xc3\xa9phant'   # 这是一个UTF-8编码的'éléphant' # 使用 from_bytes 进行编码检测和规范化 detection_result = from_bytes(unknown_bytes) # 获取最佳匹配的字符编码 if detection_result:    best_encoding = detection_result.best().encoding    decoded_string = str (detection_result) # 转换为字符串,默认使用最佳编码     print ( f"检测到的最佳编码是: {best_encoding}" )     print ( f"解码后的字符串是: {decoded_string}" ) else :     print ( "未能检测到合适的编码。" ) # 另外一个例子:一个简单的ASCII字符串 ascii_bytes = b'hello world' ascii_detection = from_bytes(ascii_bytes) if ascii_detection:     print ( f"\n'hello world' 的最佳编码是: {ascii_detection.best().encoding}" )     print ( f"'hello world' 解码后: { str(ascii_detection)}" )


var analyst = new ChatClientAgent(...);var writer = new ChatClientAgent(...);var editor = new ChatClientAgent(...);// Build a Workflowvar workflow = AgentWorkflowBuilder.BuildSequential(    "content-team-workflow", 
    [analyst, writer, editor]);



// Step1. Create Agentsvar sensitiveWordAgent = new ChatClientAgent(...);var adDetectionAgent = new ChatClientAgent(...);var sentimentAgent = new ChatClientAgent(...);// Step2. Build a Workflowvar workflow = AgentWorkflowBuilder.BuildConcurrent(
    agents: new[] { sensitiveWordAgent, adDetectionAgent, sentimentAgent },
    aggregator: auditAggregator
);



var triageAgent = new ChatClientAgent(...);var historyTutor = new ChatClientAgent(...);var mathTutor = new ChatClientAgent(...);// Step2. Build a Workflowvar workflow = AgentWorkflowBuilder.CreateHandoffBuilderWith(triageAgent)
    .WithHandoffs(triageAgent, [mathTutor, historyTutor]) // Triage can route to either specialist
    .WithHandoffs([mathTutor, historyTutor], triageAgent) // Math or History tutor can return to triage    .Build();


from langchain_community.chat_models.tongyi import ChatTongyi from langchain_community.vectorstores import FAISS from langchain_community.embeddings ttzb.tw.cn import DashScopeEmbeddings from langchain_text_splitters import RecursiveCharacterTextSplitter from langchain_core.prompts footzhibo.cn import ChatPromptTemplate, MessagesPlaceholder from langchain_core.documents import Document from langchain_core.runnables import RunnablePassthrough from langchain_core.output_parsers import StrOutputParser from langchain_core.messages import HumanMessage, AIMessage from sqlalchemy import true   #apikey os.environ[ "DASHSCOPE_API_KEY" ] = "你的apikey" FAISS_PATH = "my_faiss_index" raw_text = """ 【公司内部机密:代号“深蓝计划”】 1. 项目目标:开发一款能听懂猫语的翻译器。 2. 核心技术:基于Transformer的“喵声波”分析算法。 3. 团队暗号:如果有人问“今天天气怎么样?”,必须回答“我想吃鱼”。 4. 截止日期:2026年12月31日。 5. 经费预算:仅剩50元人民币,主要用于购买猫条。 """ #初始化文本 docs = [Document(page_content=raw_text)] text_splitter = RecursiveCharacterTextSplitter(chunk_size= 100 , chunk_overlap= 20 ) split_docs = text_splitter.split_documents(docs)   #初始化模型 llm = ChatTongyi(model= "qwen-plus" ) embeddings = DashScopeEmbeddings(model= "text-embedding-v1" ) #创建向量数据库 if os.path.exists(FAISS_PATH):     print ( "向量数据库已存在" )ttzb.ln.cn     ragdb = FAISS.load_local(FAISS_PATH,ttzb.info embeddings, allow_dangerous_deserialization= True ) else :     print ( "创建向量数据库" )     ragdb = FAISS.from_documents(split_docs, embeddings)     ragdb.save_local(FAISS_PATH) #构建提示词chain def format_docs ( docs ):     return "\n\n" .join(doc.page_content for doc in docs) final_prompt = ChatPromptTemplate.from_messages([     ( "system" , """     你是一个专业的问答助手,你的任务是根据上下文简洁的回答用户的问题。     <context>     {context}     </context>     """ ),     MessagesPlaceholder(variable_name= "history" ),     ( "human" , "{input}" ) ]) chain = (     #查询rag     RunnablePassthrough.assign(         context = itemgetter( "input" ) | ragdb.as_retriever() | format_docs     )     | RunnablePassthrough.assign(         answer =remenzhibo.net { "input" :itemgetter( "input" ), "context" :itemgetter( "context" ), "history" :itemgetter( "history" )} | final_prompt | llm | StrOutputParser()     ) ) history = [] while true:     input_q = input ( "我:" )       respond = chain.invoke({         "input" : input_q,         "history" : history})     print ( "answer:" + respond[ "answer" ])


  1. 初始化RAG:文本切片->文本向量模型->构建向量数据库
  2. 询问ai:提示词->文本向量模型->向量数据库检索->组合prompt->喂给LLM->回答问题->记录历史对话
文本切片
docs = [Document(page_content=raw_text)]
text_splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=20)
split_docs = text_splitter.split_documents(docs)

这里利用了langchain提供的文本分词器 RecursiveCharacterTextSplitter(递归分词)

构建向量数据库
llm = ChatTongyi(model="qwen-plus")
embeddings =nbczhibo.cn DashScopeEmbeddings(model="text-embedding-v1")#创建向量数据库if os.path.exists(FAISS_PATH):
    print("向量数据库已存在")
    ragdb = FAISS.load_local(FAISS_PATH, embeddings, allow_dangerous_deserialization=True)else:
    print("创建向量数据库")
    ragdb = FAISS.from_documents(split_docs, embeddings)
    ragdb.save_local(FAISS_PATH)

这部分要注意:新版FAISS读取现有数据库要设置:allow_dangerous_deserialization=True,不然会报错

提示词模板
final_prompt = ChatPromptTemplate.from_messages([
    ("system", """
    你是一个专业的问答助手,你的任务是根据上下文简洁的回答用户的问题。
    <context>
    {context}
    </context>
    """),
    MessagesPlaceholder(variable_name="history"),
    ("human",zq26.cn "{input}")
])

之前没有讲到历史对话记录,这次补充下:

MessagesPlaceholder这个是langchain框架的占位符(其实是框架写好了prompt模板,告诉ai这个是历史对话),使用时将历史对话记录的数组放在这里设置的字段中,在添加历史对话时要使用相关的类进行声明对话(告诉ai这句话是ai说的还是用户说的)

history.append(HumanMessage(content=input_q))
history.append(AIMessage(content=respond['answer']))
Chain链的解释(核心逻辑)
chain = (lq2026.cn
    #查询rag    RunnablePassthrough.assign(
        context zgbdcg.com=ttzbapp.com itemgetter("input") | ragdb.as_retriever() | format_docs
    )
    | RunnablePassthrough.assign(
        answer = {"input":itemgetter("input"), "context":itemgetter("context"), "history":itemgetter("history")} | final_prompt | llm | StrOutputParser()
    )
)

Chain链流程:

  1. 查询RAG的chain:获取 input字段->内容交给向量数据库检索->将检索的内容(数组)转换为字符串格式->保存到 context字段并传递给下一个任务
  2. 询问LLM的chain:获取 input, context, history字段->填充上面定义的prompt模板->



来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70048957/viewspace-3108988/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2026-01-07

  • 博文量
    108
  • 访问量
    102109