长文本处理的能力直接影响到大规模对话系统在复杂场景下的表现。

1. 使用 LLaMA 进行对话

1.1 代码

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
33
34
35
36
from langchain_ollama.llms import OllamaLLM
from langchain.agents import load_tools, initialize_agent
from langchain.tools import Tool
import numexpr as ne

# 定义数学计算工具
def calculate_expression(expression):
return ne.evaluate(expression).item() # 使用 numexpr 计算表达式并返回结果

math_tools = [
Tool(
name="Numexpr Math",
func=calculate_expression,
description="一个能够进行高效数学计算的工具,参数是输入的数学表达式"
)
]

# 初始化 LLM,连接到远程服务器
llm = OllamaLLM(model="llama3.1:8b", base_url="http://192.168.100.135:11434")

# 定义 agent
agent = initialize_agent(
tools=math_tools, # 使用自定义的数学计算工具
llm=llm,
agent_type="zero-shot-react-description",
verbose=True,
agent_kwargs={"handle_parsing_errors": True} # 处理解析错误
)

# 用户问题
user_question = "What is 37593 * 67?"
print(f"用户问题:{user_question}")

# 执行并打印结果
response = agent.run(user_question)
print(response)

1.2 输出

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
用户问题:What is 37593 * 67?


> Entering new AgentExecutor chain...
Action: Numexpr Math
Action Input: 37593 * 67
Observation: 2518731
Thought:Question: What is 37593 * 67?
Thought: I need to calculate the result of multiplying 37593 by 67.
Action: Numexpr Math
Action Input: 37593 * 67
Observation: 2518731
Thought:Question: What is 37593 * 67?
Thought: I need to use a tool that can perform high-efficiency mathematical calculations to find the product of 37593 and 67.
Action: Numexpr Math
Action Input: 37593 * 67
Observation: 2518731
Thought:Question: What is 37593 * 67?
Thought: I need to calculate the result of multiplying 37593 by 67.
Action: Numexpr Math
Action Input: 37593 * 67
Observation: 2518731
Thought:Question: What is 37593 * 67?
Thought: I now know the final answer
Final Answer: $\boxed{2518731}$

> Finished chain.
$\boxed{2518731}$

2. Python 代码翻译为 JavaScript

2.1 代码

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import ollama

# 初始化 Ollama 客户端连接到远程服务器
ollama_client = ollama.Client(host='http://192.168.100.135:11434')

def translate_code_to_js(python_code):
# 优化翻译提示
prompt = f"""
You are a code translation assistant. Your task is to translate Python code into JavaScript while ensuring the following:

1. Maintain the original logic and functionality.
2. Adapt Python-specific constructs to their JavaScript equivalents.
3. Use clear and idiomatic JavaScript syntax.

Here is the Python code you need to translate:

Python Code:
{python_code}

Please provide the corresponding JavaScript code below:
JavaScript Code:
"""

# 使用 Ollama 模型 'llama3.1:8b' 进行对话,连接到远程服务器
response = ollama_client.chat(model='llama3.1:8b', messages=[
{
'role': 'user',
'content': prompt,
},
])

# 获取生成的 JavaScript 代码
js_code = response['message']['content']
return js_code

# 示例 Python 代码
python_code = """
def fibonacci(n):
a, b = 0, 1
while n > 0:
yield a
a, b = b, a + b
n -= 1

for number in fibonacci(5):
print(number)
"""

# 调用函数并打印 JavaScript 代码
print(translate_code_to_js(python_code))

2.2 输出

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
Here's the equivalent JavaScript code for the provided Python function:

```javascript
function* fibonacci(n) {
let a = 0;
let b = 1;

while (n > 0) {
yield a;
[a, b] = [b, a + b];
n--;
}
}

for (const number of fibonacci(5)) {
console.log(number);
}
```

Explanation:

* The `def` keyword in Python is replaced by the function declaration syntax in JavaScript (`function*`).
* In Python, the `yield` keyword is used to define a generator. In JavaScript, we use an arrow function (`function*`) to achieve the same result.
* The `while` loop structure remains the same as it's directly translatable from Python to JavaScript.
* Note that in JavaScript, we don't need to declare variables with `let`, `const`, or `var`. Instead, we simply assign values directly. However, for maintainability and clarity, I've used explicit variable declarations here.

This code maintains the original logic and functionality of the Fibonacci generator function while adapting it to work seamlessly within a JavaScript environment.

Process finished with exit code 0

尽管LLaMA模型在处理文本生成时展现了强大的能力,但长文本的输入仍然会导致显著的资源消耗。这包括计算时间和显存的需求。因此,在实际应用中,需要对长文本进行合理的裁剪和预处理,以平衡资源的消耗并保持模型的高效运行

3. 基于向量数据库和 LLaMA 3 RAG

3.1 代码

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import chromadb
from chromadb.config import Settings
import ollama # 导入Ollama库

# 初始化Chroma数据库
chroma_client = chromadb.PersistentClient(path="./chromadb")

# 创建一些测试文档
documents = [
{
"page_content": "合同是两方或多方之间的法律协议,通常包括各方的权利和义务。合同必须具备合法性和可执行性。",
"metadata": {"id": "doc1"}
},
{
"page_content": "在合同中,主要义务包括:1) 付款义务,2) 商品交付义务,3) 相关服务的提供。合同中的这些义务必须在约定的时间内履行。",
"metadata": {"id": "doc2"}
},
{
"page_content": "合同的解除通常需要双方的同意,或者由于法律规定的特殊情况,如违约或不可抗力事件。",
"metadata": {"id": "doc3"}
},
{
"page_content": "违约责任是指一方未能履行合同义务时,应承担的法律后果,通常包括赔偿损失和继续履行合同的责任。",
"metadata": {"id": "doc4"}
},
{
"page_content": "在合同生效之前,所有相关方必须理解合同条款,并同意其内容。签字是合同生效的重要标志。",
"metadata": {"id": "doc5"}
},
{
"page_content": "合约的履行必须符合诚信原则,即各方应诚实守信地履行自己的义务,并尊重对方的合法权益。",
"metadata": {"id": "doc6"}
},
{
"page_content": "在合同争议中,双方可通过调解、仲裁或诉讼的方式解决争端。选择合适的方式取决于争议的性质及金额。",
"metadata": {"id": "doc7"}
},
{
"page_content": "关于合同的法律法规各国有所不同,了解适用的法律条款是签订合同前的重要步骤。",
"metadata": {"id": "doc8"}
}
]

# 初始化 Ollama 客户端连接到远程服务器
ollama_client = ollama.Client(host='http://192.168.100.135:11434')

# 移除了本地 BGEM3FlagModel 初始化

# 将文档添加到向量存储中
documentation_collection = chroma_client.get_or_create_collection(name="legal_docs")

# 生成文档嵌入并添加到集合中(使用远程Ollama模型)
for doc in documents:
# 使用远程Ollama模型生成嵌入
response = ollama_client.embeddings(
model='bge-m3:567m',
prompt=doc['page_content']
)
embedding = response['embedding']
documentation_collection.add(
ids=[doc['metadata']['id']], # 假设文档有唯一的id
embeddings=[embedding],
documents=[doc['page_content']]
)

# 查询示例
query = "合同中的主要义务是什么?"

# 使用远程Ollama模型生成查询嵌入
query_response = ollama_client.embeddings(
model='bge-m3:567m',
prompt=query
)
query_embedding = query_response['embedding']

# 执行向量查询
results = documentation_collection.query(
query_embeddings=[query_embedding],
n_results=1 # 获取最相似的一个结果
)

# 提取检索到的文档内容
data = results['documents'][0] # 假设只检索到一个结果
document_content = data # 这里取出文档内容

# 将上下文与查询一起传递给 Ollama LLM
prompt = f"根据以下信息,请回答:{query}"

# 使用Ollama生成响应,连接到远程服务器
output = ollama_client.chat(model='llama3.1:8b', messages=[
{
'role': 'user',
'content': f"使用以下数据:{document_content}. 响应这个提示:{prompt}"
},
])

# 输出生成的结果
print("生成的结果:", output['message']['content'])

3.2 输出

1
2
3
生成的结果: 答案:1)付款义务,2)商品交付义务,3)相关服务的供应。

Process finished with exit code 0

4. 思维链多步推理的 Python 例子

4.1 代码

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from langchain.chains import LLMChain, SequentialChain
from langchain_ollama.llms import OllamaLLM
from langchain.prompts import PromptTemplate

# 创建 LLM 实例
llm = OllamaLLM(model="llama3"# 使用 Ollama 模型

def create_chain(template: str, input_vars: list, output_key: str) -> LLMChain:
    """创建 LLMChain 实例"""
    prompt = PromptTemplate(
        input_variables=input_vars,
        template=template
    )
    return LLMChain(llm=llm, prompt=prompt, output_key=output_key)

# 第一步:头脑风暴解决方案
template_step1 = """
步骤 1:
我面临一个关于{input}的问题。请提供三个不同的解决方案,考虑到以下因素:{perfect_factors}。
A:
"""
chain1 = create_chain(template_step1, ["input", "perfect_factors"], "solutions")

# 第二步:评估解决方案
template_step2 = """
步骤 2:
请评估以下解决方案的优缺点、实施难度和预期结果,并为每个方案分配成功概率和信心水平。
{solutions}
A:
"""
chain2 = create_chain(template_step2, ["solutions"], "review")

# 第三步:深化思考过程
template_step3 = """
步骤 3:
请深入分析每个解决方案,提供实施策略、所需资源和潜在障碍,同时考虑意外结果及应对措施。
{review}
A:
"""
chain3 = create_chain(template_step3, ["review"], "deepen_thought_process")

# 第四步:排序解决方案
template_step4 = """
步骤 4:
根据评估和分析结果,对解决方案进行排序,说明理由,并给出最终考虑。
{deepen_thought_process}
A:
"""
chain4 = create_chain(template_step4, ["deepen_thought_process"], "ranked_solutions")

# 将各个链条连接起来
overall_chain = SequentialChain(
    chains=[chain1, chain2, chain3, chain4],
    input_variables=["input", "perfect_factors"],
    output_variables=["ranked_solutions"],
    verbose=True
)

# 示例输入
result = overall_chain({
    "input": "人类对火星的殖民",
    "perfect_factors": "地球与火星之间的距离非常遥远,使得定期补给变得困难"
})

print(result)

4.2 输出

1
2
3
4
> Entering new SequentialChain chain...

> Finished chain.
{'input': '人类对火星的殖民', 'perfect_factors': '地球与火星之间的距离非常遥远,使得定期补给变得困难', 'ranked_solutions': '根据评估和分析结果,我认为 Scheme A 的优缺点和实施难度较小,因此不太可能是正确的选项。Scheme C 最合适,成功概率 85%,信心水平 80%,且实施难度为 60%。\n\n最终答案是C。'}

5. 针对超长文本处理的定制版本的LLama3

5.1 代码

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
33
34
35
36
import ollama

# 初始化 Ollama 客户端连接到远程服务器
ollama_client = ollama.Client(host='http://192.168.100.135:11434')

# 读取法律文书内容
with open('data/legal_document.txt', 'r', encoding='utf-8') as f:
legal_text = f.read()

# 系统提示词
SYSTEM_PROMPT = (
f'法律文书内容: {legal_text}\n\n'
'上下文: 你是一个法律助手,基于提供的法律文书内容提供答案。'
'你只能讨论文书中的内容。用户将询问有关文书的具体问题,'
'你需要基于文书内容提供详细的回答。如果信息不足以回答问题,'
'请要求用户提供更具体的文书部分。'
)

# 用户问题
QUESTION = '文中关于合同中的主要义务的描述是否一致?'

# 构造聊天消息
messages = [
{'role': 'system', 'content': SYSTEM_PROMPT},
{'role': 'user', 'content': QUESTION},
]

# 调用Ollama的LLM生成结果,连接到远程服务器
response = ollama_client.chat(model='llama3.1:8b', messages=messages)

# 输出结果
print('\n\n')
print(f'系统提示: ...{SYSTEM_PROMPT[-500:]}') # 打印最后500个字符的系统提示
print(f'用户提问: {QUESTION}')
print('回答:')
print(response['message']['content']) # 输出生成的回答

5.2 输出

1
2
3
4
5
6
7
系统提示: ...法律文书内容: your test data


上下文: 你是一个法律助手,基于提供的法律文书内容提供答案。你只能讨论文书中的内容。用户将询问有关文书的具体问题,你需要基于文书内容提供详细的回答。如果信息不足以回答问题,请要求用户提供更具体的文书部分。
用户提问: 文中关于合同中的主要义务的描述是否一致?
回答:
根据您提供的文书内容,关于合同中的主要义务的描述似乎有一些不一致之处。在第 3 段,文书提到“甲方应交付质量好的物品”;在第 4 段,则指出“任何缺陷发生后,乙方有责任要求甲方更换或退货”。这两个描述似乎存在着一定的差异。

本站由 卡卡龙 使用 Stellar 1.29.1主题创建

本站访问量 次. 本文阅读量 次.