admin 发表于 2025-1-11 22:29:20

【音云API】让R自己解释画出的图表

<h1>背景</h1>
<p>我们已经知道Chatgpt可以帮助我们写代码,以及使用起来gpt直接融合进Rstudio的插件。</p>
<p>使用R语言做可视化,可以画出多种精美的图标。最后用于写文章时,大量的figure legend需要我们去书写,以及解释图标的结果。</p>
<p>是时候使用人工智能的武器来帮助我们解释图表了。</p>
<p>有没有可以让gpt直接描述的R包,这样我们可以直接&quot;让R自己解释画出的图表&quot;了。</p>
<p>就算有了这样的插件,又是老大难的问题来了:</p>
<p>1.科学上网;2.付费;3.频率限制。</p>
<p>是不是想起了什么?音云中转API!</p>
<p>还要介绍一个主角,ellmer包。ellmer 的目标是为最常见的llm提供商提供一个用户友好的包装器。主要设计目标包括支持流媒体以及简化注册和调用 R 函数的过程。</p>
<p>而本次介绍的项目合并了ellmer包和音云API项目,将ellmer和Chatgpt无缝衔接在一起,让你的R自己解释画出的图表。</p>
<h1>环境准备</h1>
<h2>音云API</h2>
<p>https://api.voiceclouds.cn/</p>
<p>2元对等官方1美刀的额度,这真的太香了。</p>
<p>按照网站教程,我们生成支持默认的key(所有模型都可以调用)就可以保存备用了。</p>
<h2>ellmer</h2>
<p>安装版本呢已兼容音云api。</p>
<p>安装命令</p>
<pre><code>devtools::install_github(&quot;xiehs211/ellmer&quot;,destdir = 'C:/Users/Administrator/software/R/RStudiowork/R-4.4.1/downloaded_packages')
</code></pre>
<h1>ellmer简介</h1>
<h2>ellmer函数</h2>
<p>Ellmer支持众多AI平台。需要在~/.Renviron中设置适当的环境变量,打开该文件的一种简单方法是调用usethis::edit_r_environ():对于chat_claude() ,使用https://console.anthropic.com/account/keys中的密钥设置ANTHROPIC_API_KEY 。对于chat_gemini() ,使用https://aistudio.google.com/app/apikey中的密钥设置GOOGLE_API_KEY 。对于chat_openai()使用https://platform.openai.com/account/api-keys中的密钥设置OPENAI_API_KEY。</p>
<p>这里因为我们仅修改了openai的接口,且兼容音云API的令牌。其余模型后续修改更新后也会1个令牌使用多个模型。简单易用。所以不用去各自的官网获取了。</p>
<h2>创建聊天对象chat</h2>
<p>Ellmer会使用prompt提示词,并保留对话的上下文,因此每个新查询都可以基于先前的查询。</p>
<h2>交互聊天</h2>
<p>通过调用 live_console(chat),可以在R控制台中与模型实时互动。也可以直接用代码进行聊天。</p>
<h2>相比其他GPT包</h2>
<p>ellmer相比chatgpt包、gptstudio包(前面分享过这个插件)优点在于多平台兼容、API更灵活、可识别本地图表支持多model,对话更加自由。</p>
<h1>代码实战</h1>
<h2>R代码聊天</h2>
<pre><code>library(ellmer)
chat &lt;- chat_openai(
api_key = &quot;sk-yinyunapixxxxxxxxxxxxxxxxx&quot;,
model = &quot;gpt-4o-mini&quot;,
#gpt-4o-mini
system_prompt = &quot;You are a friendly but terse assistant.&quot;,
echo = TRUE
) # api_key为音云助手的令牌
#1 以交互方式还是以编程方式
chat # ellmer对象
#---1.1 交互聊天----
live_console(chat)
# 输入Q 退出
</code></pre>
<p><img src="https://roim-picx-bpc.pages.dev/rest/opWRmmK.png" alt="1.png" /></p>
<h2>解析在线图片</h2>
<pre><code>chat$chat(content_image_url(&quot;https://www.r-project.org/Rlogo.png&quot;),
          &quot;解释这个logo?&quot;)
</code></pre>
<p><img src="https://roim-picx-bpc.pages.dev/rest/6PBsmmK.png" alt="2.png" /></p>
<h2>解析本地图片</h2>
<pre><code>chat$chat(
&quot;What do you see in these images?&quot;,
content_image_file(system.file(&quot;httr2.png&quot;, package = &quot;ellmer&quot;))
)
</code></pre>
<p><img src="https://roim-picx-bpc.pages.dev/rest/SBFYmmK.png" alt="3.png" /></p>
<p>描述森林图<br />
<img src="https://roim-picx-bpc.pages.dev/rest/WpVANmK.jpeg" alt="11234.jpg" /></p>
<pre><code>chat$chat( &quot;请从医学论文SCI角度针对图片应用统计学知识撰写result结果,并用英文描述&quot;,
         content_image_file(&quot;C:/迅雷下载/11234.jpg&quot;))
</code></pre>
<p>结果如下:<br />
<img src="https://roim-picx-bpc.pages.dev/rest/iCdBNmK.png" alt="4.png" /></p>
<p>以上示例使用的gpt-4o-mini,你也可以使用音云api支持的其他模型,定义chat后调用。</p>
<h1>对表格描述</h1>
<pre><code>chat$chat(&quot;请从医学论文SCI角度针对下列全部结果进行result统计分析详细撰写
                  Variable    Crude OR (95% CI) Crude P Value    Adjusted OR (95% CI) P (Wald's Test) P (LR Test)
1       x1: ref.=1                                                                                  0.624
2                2   3.33 (0.34,32.96)          0.303      1.26 (0.05,29.64)            0.884      
3                3   8.57 (0.83,89.04)          0.072      3.34 (0.13,89.28)            0.472      
4                4    15 (0.66,339.55)          0.089   5.26 (0.13,220.23)            0.383      
5       x2: 2 vs 1      4.09 (1.29,13)          0.017       1.42 (0.2,10.11)            0.728       0.729
6       x3: 2 vs 1   3.4 (1.11,10.4)          0.032      2.22 (0.22,22.36)            0.497       0.494
7       x4: 2 vs 1    4.26 (1.02,17.8)          0.047      5.77 (0.38,87.04)            0.205       0.179
8       x5: 2 vs 1      5 (1.52,16.46)          0.008      4.37 (0.44,43.77)             0.21       0.194
9       x6: 2 vs 1 16.87 (1.97,144.32)         0.01   15.15 (0.98,235.4)            0.052       0.024
10      x7: ref.=1                                                                                  0.944
11               2   4.12 (1.05,16.26)          0.043 0.9994 (0.1056,9.4609)                1      
12               3    4.58 (0.77,27.3)          0.094      1.71 (0.07,42.16)            0.741      
13 x9 (cont. var.)    1.05 (1.02,1.09)          0.005          1.07 (1,1.15)            0.035      0.01
          &quot;)
</code></pre>
<p><img src="https://roim-picx-bpc.pages.dev/rest/GwDbNmK.png" alt="5.png" /><br />
至此,你的R就可以自己解释自己的图表了,当然也可以解释别人文章中的图表(保存到本地再给ellmer)了。</p>
页: [1]
查看完整版本: 【音云API】让R自己解释画出的图表