[入门] [示例笔记本] [?演示] [博客文章 #1] [博客文章 #2] [论文] [围脖]
维护者:Ben Cohen-Wang、Harshay Shah 和 Kristian Georgiev
context_cite
是一个工具,用于将 LLM 生成的语句归因于上下文的特定部分。
通过pip
安装context_cite
pip install context_cite
使用context_cite
非常简单:
from context_cite import ContextCiter
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
context = """
Attention Is All You Need
Abstract
The dominant sequence transduction models are based on complex recurrent or convolutional neural networks that include an encoder and a decoder. The best performing models also connect the encoder and decoder through an attention mechanism. We propose a new simple network architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two machine translation tasks show these models to be superior in quality while being more parallelizable and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task, improving over the existing best results, including ensembles, by over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training costs of the best models from the literature. We show that the Transformer generalizes well to other tasks by applying it successfully to English constituency parsing both with large and limited training data.
1 Introduction
Recurrent neural networks, long short-term memory [13] and gated recurrent [7] neural networks in particular, have been firmly established as state of the art approaches in sequence modeling and transduction problems such as language modeling and machine translation [35, 2, 5]. Numerous efforts have since continued to push the boundaries of recurrent language models and encoder-decoder architectures [38, 24, 15].
Recurrent models typically factor computation along the symbol positions of the input and output sequences. Aligning the positions to steps in computation time, they generate a sequence of hidden states ht, as a function of the previous hidden state ht-1 and the input for position t. This inherently sequential nature precludes parallelization within training examples, which becomes critical at longer sequence lengths, as memory constraints limit batching across examples. Recent work has achieved significant improvements in computational efficiency through factorization tricks [21] and conditional computation [32], while also improving model performance in case of the latter. The fundamental constraint of sequential computation, however, remains.
Attention mechanisms have become an integral part of compelling sequence modeling and transduction models in various tasks, allowing modeling of dependencies without regard to their distance in the input or output sequences [2, 19]. In all but a few cases [27], however, such attention mechanisms are used in conjunction with a recurrent network.
In this work we propose the Transformer, a model architecture eschewing recurrence and instead relying entirely on an attention mechanism to draw global dependencies between input and output. The Transformer allows for significantly more parallelization and can reach a new state of the art in translation quality after being trained for as little as twelve hours on eight P100 GPUs.
"""
query = "What type of GPUs did the authors use in this paper?"
cc = ContextCiter . from_pretrained ( model_name , context , query , device = "cuda" )
我们可以使用cc.response
检查模型的响应:
In [ 1 ]: cc . response
Out [ 1 ]: 'The authors used eight P100 GPUs in their Transformer architecture for training on the WMT 2014 English-to-German translation task.'
模型从哪里获取信息?让我们看看属性是什么样的!
In [ 2 ]: cc . get_attributions ( as_dataframe = True , top_k = 5 )
Out [ 2 ]:
最后,让我们尝试归因于响应的特定部分。为此,我们指定与我们想要属性的响应范围相对应的start_idx
和end_idx
。在本例中,我们将指定索引来归因响应中的短语"the WMT 2014 English-to-German translation task"
。
In [ 3 ]: cc . get_attributions ( start_idx = 83 , end_idx = 129 , as_dataframe = True , top_k = 5 )
Out [ 3 ]:
使用我们的示例笔记本尝试context_cite
(您可以在 Google colab 中打开它们):
context_cite
快速介绍langchain
RAG 设置中链接context_cite
@article { cohenwang2024contextcite ,
title = { ContextCite: Attributing Model Generation to Context } ,
author = { Cohen-Wang, Benjamin and Shah, Harshay and Georgiev, Kristian and Madry, Aleksander } ,
journal = { arXiv preprint arXiv:2409.00729 } ,
year = { 2024 }
}