解码对话式交易:使用大型语言模型从交易对话中提取关键交易信息(2)

第二部分:使用小型开源LLMs进行信息提取

小型开源LLMs简介

在AI和机器学习领域,小型开源大型语言模型(LLMs)的出现,特别是对于像金融这样数据安全和保密至关重要的行业来说,已经是一个游戏规则改变者。 这些模型提供了强大的语言处理能力和本地部署的灵活性,使它们成为金融机构的理想选择。

为何小型LLMs对金融机构至关重要

  • 保密性和数据安全:金融机构处理敏感信息,包括法律和道德上禁止与外部分享的机密交易细节。 利用本地托管的LLMs确保所有数据处理在内部进行,维护数据保密性并遵守严格的监管标准。
  • 定制化和控制:开源LLMs提供了根据特定机构需求定制化的灵活性。 金融机构可以调整这些模型,更好地理解和处理其独特的金融术语和交易语言,增强模型的相关性和准确性。
  • 成本效益和可访问性:较小的LLMs资源需求较低,为可能没有支持像GPT-4这样的大型模型的基础设施的金融机构提供了更可访问和成本效益更高的解决方案。 这使得先进的AI技术可为更广泛的机构所用,实现对尖端工具的普及。

利用小型开源LLMs提取交易信息的过程

我们把利用LLMs从金融交易对话中提取有意义的信息分为两个步骤。 第一步识别对话中的交易类型,然后使用为每种交易类型量身定做的模板提取特定的交易信息。 本篇文章说明如何识别交易类型,下一篇介绍如何提取交易信息。

步骤一:识别交易类型

第一个关键步骤涉及从文本对话数据中确定交易类型。 过程如下:

    {
        "model": "gpt-4-1106-preview",
        "trade_type": "FX Double Barrier Knock-In Option",
        "currency": "NZDUSD",
        "trader1": {
            "name": "Alice",
            "style": "descriptive",
            "emotion": "skeptical",
            "tone": "neutral",
            "attitude": "open-minded",
            "perspective": "first-person"
        },
        "trader2": {
            "name": "Bob",
            "style": "conversational",
            "emotion": "happy",
            "tone": "passive",
            "attitude": "contrary",
            "perspective": "local"
        },
        "deal": {
            "trade_type": "FX Double Barrier Knock-In Option",
            "currency_pair": "NZDUSD",
            "lower_barrier": "1.2500",
            "upper_barrier": "1.3500",
            "strike_price": "1.3000",
            "expiry_date": "2024-07-01",
            "option_type": "Call",
            "premium": "0.0180",
            "amount": "500000",
            "buyer": "Alice",
            "seller": "Bob",
            "conversation": [
                {
                    "name": "Alice",
                    "message": "I'm exploring the idea of a double barrier knock-in option on NZDUSD. I want the lower and upper barriers set at 1.2500 and 1.3500, respectively, with a strike price at 1.3000."
                },
                {
                    "name": "Bob",
                    "message": "Oh, how delightful! A double barrier knock-in option, you say? Setting the lower barrier at 1.2500 and the upper one at 1.3500 sounds like an adventure. And the strike at 1.3000, correct?"
                },
                {
                    "name": "Alice",
                    "message": "Correct. The focus should be on protecting the position, considering the volatility in the NZDUSD pair. Do you have any thoughts on the premium and amount?"
                },
                {
                    "name": "Bob",
                    "message": "Well, if we're taking a stroll through this garden, I'd say a premium of 0.0180 for an amount of 500,000 sounds like the right path to take. It's congenial, yet considerate of the risks."
                },
                {
                    "name": "Alice",
                    "message": "That premium seems fair, given the constraints of the barriers. Let's formalize this option with an expiry of July 1st, 2024. Does that fit within your timeline?"
                },
                {
                    "name": "Bob",
                    "message": "Indeed, Alice, July 1st, 2024, as the expiry date is like the perfect time for this option to blossom. It's all coming together quite nicely."
                },
                {
                    "name": "Alice",
                    "message": "Great. We'll put this FX double barrier knock-in call option on NZDUSD in motion then. Just to recap: lower barrier at 1.2500, upper barrier at 1.3500, strike price at 1.3000, expiry date on 2024-07-01, option type 'Call', premium at 0.0180, for the amount of 500,000."
                },
                {
                    "name": "Bob",
                    "message": "What a splendid summary! Our little financial seedling is ready to be planted. I'll get the paperwork started. Pleasure doing this dance with you."
                }
            ]
        }
    }
  • 对话格式化:我们首先将原始对话数据格式化为适合LLM处理的连续文本流。
def conversation_to_text(conversation):     
    text = ""     
    for message in conversation:         
        text += message['name'] + ": " + message['message'] + "\n"     
    return text
  • 交易类型定义:定义了21种不同类型的金融交易并由GPT-4生成模板。
trade_types=['FX Spot','FX Swap','FX Vanilla Option',
    'FX Down and In Option', 'FX Down and Out Option', 
    'FX Up and In Option', 'FX Up and Out Option', 
    'FX Double Barrier Knock-In Option', 
    'FX Double Barrier Knock-Out Option',
    'FX Range Accrual Option','Fixed-Floating IRS',
    'Floating-Floating IRS','IRO Cap','Fixed-Fixed IRS',
    'Stock','Single Stock Option','Bond','Commodity',
    'Credit Default Swap','Equity Swap','Autocallable Swap']
trade_template={
  "FX Spot": {
    "trade_type": "FX Spot",
    "currency_pair": "EURUSD",
    "rate": "1.1800",
    "amount": "1000000",
    "trade_date": "2023-01-15",
    "settlement_date": "2023-01-17",
    "buyer": "Alice",
    "seller": "Bob",
    "conversation": [{"name": "Alice", "message": "Looking to buy EURUSD at 1.1800"}, {"name": "Bob", "message": "Confirmed, selling EURUSD at 1.1800"}]
  },
  "FX Swap": {
    "trade_type": "FX Swap",
    "near_leg": {
      "currency_pair": "USDJPY",
      "near_rate": "110.00",
      "near_amount": "500000",
      "near_date": "2023-01-15"
    },
    "far_leg": {
      "currency_pair": "USDJPY",
      "far_rate": "110.25",
      "far_amount": "500000",
      "far_date": "2023-06-15"
    },
    "buyer": "Alice",
    "seller": "Bob",
    "conversation": [{"name": "Alice", "message": "Looking to do a near/far swap on USDJPY"}, {"name": "Bob", "message": "Agreed on rates 110.00 and 110.25"}]
  }
  # templates for other trade types
]
  • LLM 提示用于交易类型识别:格式化对话后,将其输入 LLM 并提示以识别交易类型。
trade_types_str=", ".join(map(str,trade_types))
question = "Given trade type is one of "+ trade_types_str + ". 
    return only in a valid JSON format contains 1 elements 
    {""trade_type"":""Type""}.
    Identify the trade type from the following conversation: \n\n" 
    + conversation_text 
# response = LLM_API_Call(question)  
# API call to LLM
  • 对响应进行后处理:在提示 LLM 识别交易类型并提取特定交易信息后,我们收到的回应有时并非立即可用的格式。 这就是后处理变得至关重要的地方。 它涉及将 LLM 的输出细化为可有效分析和利用的结构化数据。 以下是我们如何处理这一关键步骤:
  1. 提取核心回应:
    LLM 的输出可能包含与我们的分析无关的额外文字或格式。 我们首先隔离核心回应 — — 我们 需要的实际信息。 例如,如果我们正在寻找一个 JSON 对象,我们会提取符合 JSON 格式的回应部分。
def extract_core_response(answer):     
    start = answer.find('{')     
    end = answer.rfind('}') + 1     
    return answer[start:end]
  1. 清理和格式化:
    LLM 输出可能包括转义字符、错放的符号或不一致的格式。 我们的下一步是清理这些异常,以确保数据处于可用格式。
def clean_response(response):     
    cleaned = response.replace("\\n", " ").replace("\\_", "_")     
    return cleaned
  1. 转换为结构化数据:
    特别是对于财务数据,首选结构化格式,如 JSON。 我们将清理过的回应转换为 JSON,处理可能出现的解析错误。
import json  
def convert_to_json(cleaned_response):     
     try: 
        result = json.loads(cleaned_response)
     except json.JSONDecodeError:         
        # Handle JSON conversion errors, possibly using regex or other methods
        result = handle_parsing_error(cleaned_response) 
     return result
  1. 处理解析错误:
    在标准 JSON 解析失败的情况下,我们实施额外的错误处理机制,如使用正则表达式来纠正常见的格式问题。
import re  
def handle_parsing_error(response):
     # Fix common JSON formatting issues using regex  
     fixed_response = re.sub('pattern_to_fix', 'replacement', response)
     try: 
         return json.loads(fixed_response)
     except json.JSONDecodeError:
         return {'error': 'Failed to parse JSON', 'original_response': response}
  1. 验证和确认数据:
    一旦我们将数据转换为 JSON 格式,我们就根据预期的结构对其进行验证,并确认其准确性。 此步骤对于确保数据与我们的财务模型和分析要求相符非常重要。
         if "trade_type" in processed:
            df_types.loc[index,model+"_type"]=processed["trade_type"]
            df_types.loc[index,model]=processed["trade_type"]==df_deals["trade_type"][index]
            if df_types.loc[index,model]:
                df_types.loc[index,"Matches"]+=1
            else:
                df_types.loc[index,"NotMatches"]+=1
         else:
            if retries==1:
                df_types.loc[index,"NaN"]+=1

模型性能分析

ModelMatchesNotMatchesNaN
Mistral38721825
Llama2:13b3003300
Orca2:13b48713112
Neural-Chat42116544
Deepseek-LLM35226314

我们对各种大型语言模型(LLM)的实验得出以下初步结论:

  • 整体性能:不同模型在正确识别交易类型方面表现各异。 Orca2:13b 的匹配率最高,其次是 Neural-Chat 和 Deepseek-LLM。
  • 不一致性和挑战:所有模型都面临一些挑战,如不匹配的数量和无法明确识别交易类型(无法辨识)的实例所示。
  • 结果解释:Orca2:13b 的高匹配率表明其在理解金融对话方面的优越能力,而像 Llama2:13b 这样的模型较低的无法辨识计数表明了更明确的回应模式,尽管不匹配数量较高。

模型性能结果的解读

值得注意的是,上表中的结果是基于一项简单且初步的实验,主要目的在说明使用大型语言模型从对话中提取交易信息的过程。 这些发现并不代表对模型性能的全面或科学评估。 以下几个因素会影响到结果:

  • 实验设计:这项实验是一项快速且简单的测试,目的展示使用LLM进行金融对话分析的可行性和方法论,而不是对模型能力进行严格评估。
  • 样本变异性:结果取决于测试中使用的特定样本集。 不同的对话数据集可能产生不同的结果,突出了数据集的性质和组成对模型性能的影响。
  • 模型不一致性:基于复杂算法和大数据集的LLM尤其可能在其性能上表现出变异性。 同一模型在不同的测试条件下或甚至在使用相同数据重新测试时可能会产生不同的结果,这是由于这些AI系统固有的随机性质。
  • 测试的目的:这项实验的主要目的是阐明在金融背景下应用LLM的过程,而不是确定性地排名模型的有效性。 结果应被视为LLM在这一应用领域的潜力的指示,而不是其相对性能的确凿证据。

第二部分第一步的结论

随着我们结束使用大型语言模型(LLM)进行金融数据提取的探索的这一部分,我们已经成功地完成了初始步骤:从对话数据中识别交易类型。 这一阶段在进行详细分析的基础设置中至关重要,展示了LLM在解读复杂金融对话中的实际应用和潜力。
到目前为止,我们的实验涉及格式化对话数据,提示LLM将这些数据分类为不同的交易类型,然后处理输出以获取可操作的信息。 我们最初实验的结果提供了各种LLM在理解和分类金融对话方面的能力初步认识。

下一步:提取交易信息

我们的下一部分将更深入地探讨这一过程的第二步。 在正确识别交易类型的基础上,我们将探索如何利用LLM从这些对话中提取特定交易信息。 这一步是真正的魔法发生之处 — 将原始数据转化为精确、有价值的洞察,以驱动金融背景下的决策制定。

  • 21
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值