
Llama para economistas
Janeiro de 2025.
O Llama é um modelo de linguagem generativo lançado pela Meta. Diferentemente de serviços pagos como ChatGPT, Gemini, Claude, Copilot ou Grok, o Llama pode ser baixado e executado em um computador pessoal. A execução local elimina os custos de API por requisição e os limites de tokens de entrada e de saída dos serviços comerciais. Um PDF de 250 páginas, por exemplo, pode exceder o limite de entrada de um serviço comercial, e respostas longas podem exceder seu limite de saída.
Este guia aplica uma instalação local do Llama a uma tarefa que rapidamente atingiria esses limites. A seção 1 trata da instalação; a seção 2, da tarefa de classificação e do código em R; e a seção 3, dos resultados. Todas as análises foram executadas em um MacBook Air M1 com 8 GB de RAM e SSD NVMe de 256 GB.
1. Instalação
# Llama installation guide
## Requirements
- Operating system: Windows 10/11, macOS, or Linux
- At least 16 GB of RAM (32 GB recommended)
- At least 10 GB of free disk space
- Python 3.9 or later
- Git
## Step 1: Prepare the environment
mkdir llama-project
cd llama-project
python -m venv llama-env
# Windows
llama-env\Scripts\activate
# macOS/Linux
source llama-env/bin/activate
## Step 2: Clone the repository
git clone https://github.com/facebookresearch/llama.git
cd llama
## Step 3: Install dependencies
pip install -r requirements.txt
pip install torch torchvision torchaudio
## Step 4: Download the model
# Request access to the models on the Meta AI website.
# After approval, download the model and extract it to ./models.
## Step 5: Configure
# config.json
{
"model_path": "./models/llama-2-7b",
"max_seq_len": 512,
"max_batch_size": 32
}
## Step 6: Test
python3 example.py
## Troubleshooting
# CUDA not found: check for a compatible NVIDIA GPU, install current CUDA drivers,
# and reinstall PyTorch with CUDA support.
# Out of memory: reduce the batch size, close other programs, or use a smaller model.
# Import errors: activate the virtual environment and reinstall the dependencies.
2. Classificação de estudos que utilizam modelos quantitativos de comércio
2.1. Amostra
A tarefa formula duas perguntas sobre cada artigo: o artigo menciona o método exact-hat algebra? E o utiliza? A amostra é composta pelos 15 primeiros resultados válidos de uma busca no Google Scholar por “quantitative trade model”. Os artigos estão listados ao final deste guia. O mesmo procedimento se aplica a amostras de 100 ou 200 artigos.
2.2. Código em R
O script lê cada PDF, realiza sua tokenização, envia o texto ao Llama com a consulta por meio da API do Ollama e organiza a resposta.
# Instala os pacotes necessários
required_packages <- c("pdftools", "httr", "jsonlite", "stringr", "dplyr", "tidyr")
new_packages <- required_packages[!(required_packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)
library(pdftools) # read PDFs
library(httr) # HTTP requests
library(jsonlite) # JSON
library(stringr) # strings
library(dplyr) # data manipulation
library(tidyr) # data tidying
# Pré-processa o texto: minúsculas, remoção de caracteres especiais, normalização de espaços
preprocess_text <- function(text) {
text <- tolower(text)
text <- stringr::str_replace_all(text, "[^[:alnum:][:space:]]", " ")
text <- stringr::str_squish(text)
return(text)
}
# Tokeniza o texto
tokenize_text <- function(text) {
tokens <- unlist(stringr::str_split(text, "\\s+"))
return(tokens)
}
# Verifica se o serviço Ollama está em execução
check_ollama_connection <- function() {
tryCatch({
response <- httr::GET("http://localhost:11434/api/tags")
return(httr::status_code(response) == 200)
}, error = function(e) {
return(FALSE)
})
}
# Classifica um documento por meio da API do Ollama
classify_document <- function(text) {
prompt <- "INSTRUCTION:
YOU MUST ANSWER EXACTLY IN THIS FORMAT:
FORMAT:
First line: exacthat (YES or NO)
Second line: exacthatuse (YES or NO)
Third line onwards: text of maximum 150 words justifying the answers
VALID RESPONSE EXAMPLE:
YES
NO
The article mentions the exact-hat algebra method in its literature review, citing it as one of the existing approaches for equilibrium analysis. However, the article does not apply this method in its empirical analysis, opting instead to use a different approach based on spatial econometrics.
RULES:
Answer if the text mentions any of these variations: exacthat algebra, exact-hat algebra, exact hat algebra, or any combination of these terms with different spacing or hyphenation
Answer if the text effectively uses any of the above variations of exact hat algebra as an analytical method
Justify your answers in up to 150 words
TEXT:
"
# Limita o tamanho do texto e o combina com o prompt
text_limited <- substr(text, 1, 100000)
full_prompt <- paste0(prompt, text_limited)
url <- "http://localhost:11434/api/generate"
body <- list(
model = "llama3.2:latest",
prompt = full_prompt,
stream = FALSE
)
tryCatch({
response <- httr::POST(
url,
body = jsonlite::toJSON(body, auto_unbox = TRUE),
httr::add_headers("Content-Type" = "application/json")
)
if (httr::status_code(response) == 200) {
result <- httr::content(response, "parsed")$response
print(paste("Raw response:", result))
lines <- unlist(strsplit(result, "\n"))
exacthat <- trimws(toupper(lines[1])) == "YES"
exacthatuse <- trimws(toupper(lines[2])) == "YES"
explanation <- paste(lines[-(1:2)], collapse = "\n")
return(list(
exacthat = exacthat,
exacthatuse = exacthatuse,
explanation = explanation
))
} else {
return(list(
exacthat = FALSE,
exacthatuse = FALSE,
explanation = "Error: API request failed",
error = "API request failed"
))
}
}, error = function(e) {
return(list(
exacthat = FALSE,
exacthatuse = FALSE,
explanation = paste("Error:", as.character(e)),
error = as.character(e)
))
})
}
# Processa um único PDF
process_document <- function(file_path) {
tryCatch({
if (!check_ollama_connection()) {
stop("Could not connect to Ollama. Check that the service is running.")
}
text <- pdftools::pdf_text(file_path)
text <- paste(text, collapse = " ")
processed_text <- preprocess_text(text)
tokens <- tokenize_text(processed_text)
classification <- classify_document(processed_text)
return(list(
file_path = file_path,
exacthat = classification$exacthat,
exacthatuse = classification$exacthatuse,
explanation = classification$explanation,
token_count = length(tokens),
sample_tokens = head(tokens, 10),
status = "success"
))
}, error = function(e) {
return(list(
file_path = file_path,
status = "error",
error_message = as.character(e)
))
})
}
# Processa vários PDFs e, opcionalmente, salva um CSV
process_multiple_documents <- function(file_paths, output_file = NULL) {
results <- lapply(file_paths, process_document)
results_df <- bind_rows(lapply(results, function(x) {
if (x$status == "success") {
data.frame(
file_path = x$file_path,
status = x$status,
exacthat = as.numeric(x$exacthat),
exacthatuse = as.numeric(x$exacthatuse),
explanation = x$explanation,
token_count = x$token_count,
sample_tokens = paste(x$sample_tokens, collapse = ", ")
)
} else {
data.frame(
file_path = x$file_path,
status = x$status,
exacthat = NA,
exacthatuse = NA,
explanation = NA,
token_count = NA,
sample_tokens = NA,
error_message = x$error_message
)
}
}))
if (!is.null(output_file)) {
write.csv(results_df, output_file, row.names = FALSE)
}
return(results_df)
}
# Exemplo: substitua o caminho pela pasta que contém os PDFs
files <- list.files("path_to_pdf_files", full.names = TRUE)
results <- process_multiple_documents(files, "analysis_results.csv")
print(results)
3. Resultados
3.1. Exemplo de resposta
A resposta para Mei (2020):
exacthat: 0
exacthatuse: 0
The text appears to be discussing various aspects of international trade,
economic policy, and game theory. Here are some possible summary points:
1. Trade models: The text discusses different types of trade models, including
infinite punishment, finite punishment, and punishment with finite periods.
2. Nash equilibrium: The authors discuss the concept of Nash equilibrium in
repeated games, where countries cooperate on tariffs to maximize their welfare.
3. Sustainability of cooperative equilibria: The text explores the conditions
under which cooperative equilibria are sustainable, including the minimum
patience parameter and the length of the punishment phase.
4. Tariff data: The authors use various sources of tariff data, including the
Global Trade Analysis Project (GTAP) database and the US export data.
5. Trade policy analysis: The text discusses the importance of quantitative
trade policy analysis in understanding the impact of trade policies on
economic outcomes.
O modelo classificou o artigo corretamente nas duas perguntas, mas a justificativa é um resumo genérico, e não uma resposta fundamentada ao prompt.
4. Conclusão
O exercício extrai informações estruturadas de um conjunto de artigos em poucos minutos. Ele pode servir como primeiro filtro em uma revisão de literatura, antes da leitura manual, mais demorada.
Referências
- Adao, R., Costinot, A., and Donaldson, D. (2022). Putting quantitative trade models to the test: evidence from the Trump tariffs. Mimeo.
- Bekkers, E., and Koopman, R. B. (2022). Simulating the trade effects of the COVID-19 pandemic: scenario analysis based on quantitative trade modelling. The World Economy, 45(2), 445–467.
- Bekkers, E., and Rojas-Romagosa, H. (2019). The welfare effects of free trade agreements in quantitative trade models: a comparison of studies about Transatlantic Trade and Investment Partnership. The World Economy, 42(1), 87–121.
- Blank, S., Egger, P. H., Merlo, V., and Wamser, G. (2022). A structural quantitative analysis of services trade de-liberalization. Journal of International Economics, 137, 103605.
- Che, Z., Che, J., and Wang, S. (2024). The gains from changes in internal trade costs: a quantitative analysis of China. Journal of Regional Science.
- Egger, P. H., and Erhardt, K. (2024). Heterogeneous effects of tariff and nontariff trade-policy barriers in quantitative general equilibrium. Quantitative Economics, 15(2), 453–487.
- Felbermayr, G., Gröschl, J. K., and Heiland, I. (2018). Undoing Europe in a new quantitative trade model. Ifo Working Paper 250.
- Huang, X., and Chen, X. (2022). A quantitative model of international trade based on deep neural network. Computational Intelligence and Neuroscience, 2022(1), 9811358.
- Krebs, O., and Pflüger, M. (2018). How deep is your love? A quantitative spatial analysis of the transatlantic trade partnership. Review of International Economics, 26(1), 171–222.
- Lee, K. Y. (2018). A quantitative trade model with unemployment. KIEP Research Paper, Working Paper 18-04.
- Mei, Y. (2020). Sustainable cooperation in international trade: a quantitative analysis. Journal of International Economics, 123, 103305.
- Ritel, M. (2022). A quantitative analysis of trade cooperation over three decades. Available at SSRN 4252542.
- Thoenig, M. (2023). Trade policy in the shadow of war: a quantitative toolkit for geoeconomics. Centre for Economic Policy Research.
- Walter, T. (2022). Applied policy research through the lens of new quantitative trade models.
- Yu, Z., Han, J., Shi, X., and Yang, Y. (2024). Estimates of the trade and global value chain (GVC) effects of China’s Pilot Free Trade Zones: a research based on the quantitative trade model. Journal of the Asia Pacific Economy, 1–48.