forked from Yeinm/finance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinance_text_matching.py
80 lines (71 loc) · 3.23 KB
/
finance_text_matching.py
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
from rich import print
from transformers import AutoTokenizer, AutoModel
import os
# 提供相似,不相似的语义匹配例子
examples = {
'是': [
('公司ABC发布了季度财报,显示盈利增长。', '财报披露,公司ABC利润上升。'),
],
'不是': [
('黄金价格下跌,投资者抛售。', '外汇市场交易额创下新高。'),
('央行降息,刺激经济增长。', '新能源技术的创新。')
]
}
def init_prompts():
"""
初始化前置prompt,便于模型做 incontext learning。
"""
pre_history = [
(
'现在你需要帮助我完成文本匹配任务,当我给你两个句子时,你需要回答我这两句话语义是否相似。只需要回答是否相似,不要做多余的回答。',
'好的,我将只回答”是“或”不是“。'
)
]
for key, sentence_pairs in examples.items():
# print(f'key-->{key}')
# print(f'sentence_pairs-->{sentence_pairs}')
for sentence_pair in sentence_pairs:
sentence1, sentence2 = sentence_pair
# print(f'sentence1-->{sentence1}')
# print(f'sentence2-->{sentence2}')
pre_history.append((f'句子一:{sentence1}\n句子二:{sentence2}\n上面两句话是相似的语义吗?',
key))
return {"pre_history": pre_history}
def inference(
sentence_pairs: list,
custom_settings: dict
):
"""
推理函数。
Args:
model (transformers.AutoModel): Language Model 模型。
sentence_pairs (List[str]): 待推理的句子对。
custom_settings (dict): 初始设定,包含人为给定的 few-shot example。
"""
for sentence_pair in sentence_pairs:
sentence1, sentence2 = sentence_pair
sentence_with_prompt = f'句子一: {sentence1}\n句子二: {sentence2}\n上面两句话是相似的语义吗?'
response, history = model.chat(tokenizer, sentence_with_prompt, history=custom_settings['pre_history'])
print(f'>>> [bold bright_red]sentence: {sentence_pair}')
print(f'>>> [bold bright_green]inference answer: {response}')
# print(history)
if __name__ == '__main__':
#device = 'cuda:0'
device = 'cpu'
tokenizer = AutoTokenizer.from_pretrained("/Users/ligang/PycharmProjects/llm/ChatGLM-6B/THUDM/chatglm-6b-int4",
trust_remote_code=True)
#model = AutoModel.from_pretrained("./ChatGLM-6B/THUDM/chatglm-6b",
# trust_remote_code=True).half().cuda()
model = AutoModel.from_pretrained("/Users/ligang/PycharmProjects/llm/ChatGLM-6B/THUDM/chatglm-6b-int4",
trust_remote_code=True).float()
model.to(device)
sentence_pairs = [
('股票市场今日大涨,投资者乐观。', '持续上涨的市场让投资者感到满意。'),
('油价大幅下跌,能源公司面临挑战。', '未来智能城市的建设趋势愈发明显。'),
('利率上升,影响房地产市场。', '高利率对房地产有一定冲击。'),
]
custom_settings = init_prompts()
inference(
sentence_pairs,
custom_settings
)