imd 统计教程 · AI科研与SCI写作
← 教程首页 返回本模块

R免费运行deepseek chatGPT等AI大模型

聂志强实战医学统计2025-02-12 17:01:00广东


各位元宵节快乐!我在去年写过一篇文章专门介绍Hadley Wickham出品的ChatGPT elmer包,含API获取方法。elmer相比chatgpt包、gptstudio包优点在于多平台兼容、API更灵活、可识别本地图表支持多model,对话更加自由。随着近期deepseekR1的爆火,这个包也升级成了ellmer,没错多了个l,图标还是原来的味道。网址,https://ellmer.tidyverse.org,此次升级了诸多模型,推荐deepseek、openai、claude。chat_openai()默认使用GPT-4o,但可以使用model = "gpt-4o-mini"来获得更便宜、更低质量的模型,或者使用model = "o1-mini"来获得更复杂的推理。

1. ellmer包

ellmer包的openAI的API引用来自https://platform.openai.com/docs/api-reference/chat/create。总体在自动撰写图表结果方面远远强于现有全部R包,默认API模型使用的是官网GPT4o。


1.1 交互聊天

1.2 图片与表格图片识别

ellmer经过我的测试可以准确读图,并且一部分正确读取表格,当然不如直接丢进去GPT4o里面。优点是,直接从jpg、png转为dataframe。ellmer 不可以直接提取rstudio软件实时的plot,但是可以直接抓取data. Frame内容进行解读,比report包好很多,report包不支持自定义表格。期待后续作者更新更优秀的功能,尤其是PDF支持问题。

Ellmer相关智荟R代码如下


#---安装ellmer----
install.packages("ellmer")
## install.packages("pak")
##pak::pak("tidyverse/elmer")
##usethis::edit_r_environ()
##shiny、fastmap、bslib这三个包都手动重新安装

#---1. 智荟R代码聊天----
library(ellmer)
chat <- ellmer::chat_openai(
  base_url = "https://api.openai.com/v1",
  api_key = "这里填写openAI的 API号",
  ## 默认4o
  model = "gpt-4o",
  #gpt-4o-mini
  system_prompt = "You are a friendly but terse assistant.",
  echo = TRUE
)

#----deepseek 更换 硅基流动!
 chat <- chat_deepseek( 
   ##硅基流动用不了 base_url = "https://api.siliconflow.cn/v1",
   base_url = "api.deepseek.com",
   api_key = " 这里写deepseek的API,目前不能购买",
   ## 默认4o
   model = "r1",
   #gpt-4o-mini
   system_prompt = "You are a friendly but terse assistant.",
   echo = TRUE
 )


#---1.1 交互聊天----
live_console(chat)

chat$chat("tabPFN是什么方法?")
chat$chat("Plots窗口中图形是什么图")
#查看token使用量
token_usage()


#-----1.2 在线图片识别-----
# 提取聊天生成的内容,包含图片URL,并将其解析为特定类型的数据
# 使用 `extract_data` 函数从 chat 对象中提取数据
# `content_image_url` 用于提取包含图像URL的内容
# URL 是指向 R 项目 logo 图片的链接
chat$extract_data(
  content_image_url("https://www.r-project.org/Rlogo.png"),  # 提供图像 URL
  type = type_object(  # 指定返回的数据类型
    primary_shape = type_string(),  # 期望从图像提取主形状(作为字符串)
    primary_colour = type_string()  # 期望从图像提取主颜色(作为字符串)
  )
)


#-----1.3 本地表格识别----
#需要前置设定变量类型
type_asset <- type_object(
  assert_name = type_string(),
  owner = type_string(),
  location = type_string(),
  asset_value_low = type_integer(),
  asset_value_high = type_integer(),
  income_type = type_string(),
  income_low = type_integer(),
  income_high = type_integer(),
  tx_gt_1000 = type_boolean()
)
type_assets <- type_array(items = type_asset)

##chat <- chat_openai()
image <- content_image_file("D:/聂个人文件/我的公众号/AI科研系列/elmer/congressional-assets.png")
data <- chat$extract_data(image, type = type_assets)
data
str(data)

2. shiny.ollama本地电脑部署

shiny.ollama 是一个 shiny 交互式的人工智能大语言模型的工具,可以选择不同模型交互,支持进行本地部署安装好ollama对应的deepseek版本,然后去cran下载shiny.ollama,就可以直接本地用起来了。

智荟R代码

# git上安装更新
##install.packages("devtools")
devtools::install_github("ineelhere/shiny.ollama")
##install.packages("shiny.ollama")
library(shiny.ollama)
# Start the application
shiny.ollama::run_app()
#--8g GPU 下载8b,CMD运行
###ollama run deepseek-r1:8b

3. 硅基流动API

网上代码改写的有点问题,无法复现,不知道啥我API的问题吗。能运行的小伙伴文末留言,看看错在哪里?

library(httr)
library(jsonlite)

# API地址
url <- "https://api.siliconflow.cn/v1/chat/completions"

# 用户输入的内容
messagetmp <- "模仿李白写一首诗"
system_prompt <- '#role: "Assistant (DeepSeek-R1-Immuno)"author: "DeepSeek"'

# 请求数据
payload <- list(
  model = "deepseek-ai/DeepSeek-R1",
  messages = list(
    list(role = "system", content = system_prompt),
    list(role = "user", content = messagetmp)
  )
)

# 设置请求头
headers <- add_headers(
  Authorization = "Bearer 这里放API",
  `Content-Type` = "application/json"
)

# 发送POST请求
response <- POST(url, body = toJSON(payload, auto_unbox = TRUE), config = headers) 
# 获取返回的响应内容
content_data <- content(response, "parsed", simplifyVector = TRUE)

# 获取并打印生成的文本内容
if (!is.null(content_data$choices)) {
  full_content <- content_data$choices[[1]]$message$content
  cat(full_content)
else {
  cat("没有返回内容,请检查请求或API响应.")
}

小结

ellmer包与R互动未来可期,shiny.ollama免费离线用适合隐私数据。


本公众号建立了学术交流群(群6),仅供SCI学术交流,人数有限需要实名制。入群请加笔者微信 popnie请备注说明:姓名-学校(单位)-专业