https://github.com/GabrielKaiserQFin/PerplexR
This package offers R users an intuitive interface for the Perplexity API.
https://github.com/GabrielKaiserQFin/PerplexR
Last synced: 4 months ago
JSON representation
This package offers R users an intuitive interface for the Perplexity API.
- Host: GitHub
- URL: https://github.com/GabrielKaiserQFin/PerplexR
- Owner: GabrielKaiserQFin
- License: gpl-3.0
- Created: 2024-01-21T18:29:17.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-12-02T14:30:16.000Z (4 months ago)
- Last Synced: 2024-12-02T15:32:42.915Z (4 months ago)
- Language: R
- Size: 383 KB
- Stars: 3
- Watchers: 0
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- License: LICENSE.md
Awesome Lists containing this project
- jimsghstars - GabrielKaiserQFin/PerplexR - This package offers R users an intuitive interface for the Perplexity API. (R)
README
---
output: github_document
---```{r, include = FALSE}
library(perplexR)
knitr::opts_chunk$set(
collapse = TRUE,
comment = "",
fig.path = "man/figures/README-",
out.width = "100%"
)```
[](https://CRAN.R-project.org/package=perplexR)
[](https://cran.rstudio.com/web/packages/perplexR/index.html)
[](https://github.com/GabrielKaiserQFin/PerplexR/actions/workflows/R-CMD-check.yaml)The objective of `perplexR` is to offer R users an intuitive interface for leveraging the capabilities of the Perplexity API [Pro subscription](https://docs.perplexity.ai/guides/getting-started). Utilizing the supplied functions, users can enhance their programming productivity by incorporating Large Language Models. Furthermore, `perplexR` includes RStudio add-ins, enabling seamless interactive integration of Perplexity prompts.
## Installation
You can install the development version of perplexR from [GitHub](https://github.com/GabrielKaiserQFin/perplexR) with:
```{r eval = FALSE}
# install.packages("devtools")
devtools::install_github("GabrielKaiserQFin/perplexR")
```## Requirements
If you have a Pro subscription, you can create an API key by accessing [Perplexity API page](https://www.perplexity.ai/settings/api), otherwise you can not use `{perplexR}`.
To use your API key in `{perplexR}`, you need to configure it in the .Renviron file, by executing `usethis::edit_r_environ()` and add the following line:
```{r eval = FALSE}
PERPLEXITY_API_KEY=XX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
```Otherwise you can also do this on a session-by-session basis by performing the following step:
```{r eval = FALSE}
Sys.setenv(PERPLEXITY_API_KEY = "XX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
```## Examples
### Addin:
In RStudio go to **Addins** and select **Ask Large Language Model**, which will open an interactive prompt in your Viewer, where you can choose between different Large Language Models and adjust the temperature and the number of tokens to be used.
Just ask anything of you interest and hit the confirm button. The **Done** button lets you close the app.
Similarly, you can mark or copy any text or code and use the other add-ins as described below. Add-in shortcuts will improve your efficiency even further.### AskMe:
This is a basic example which shows you how to ask any question.
```{r Example AskMe, cache=TRUE}
AskMe("What do you think about Large language models?")
```### rewriteText:
This is a basic example which shows you how to rewrite text.
```{r Example rewriteText, cache=TRUE}
rewriteText("Dear Recipient, I hope this message finds you well.")
```### translateText:
This is a basic example which shows you how to translate text.
```{r Example translateText, cache=TRUE}
translateText("Dear Recipient, I hope this message finds you well.", toLanguage = "Spanish")
```### annotateCode:
This is a basic example which shows you how to annotate code.
```{r Example annotateCode, cache=TRUE}
annotateCode("z <- function(x) scale(x)^2")
```### buildUnitTests:
This is a basic example which shows you how to build Unit Tests for your function.
```{r Example buildUnitTests, cache=TRUE}
buildUnitTests("z <- function(x) scale(x)^2")
```### clarifyCode:
This is a basic example to clarify your code.
```{r Example clarifyCode, cache=TRUE}
clarifyCode("for (i in 1:10) {\n print(i ** 2)\n}")
```### debugCode:
This is a basic example which shows you how to find bugs in your code.
```{r Example debugCode, cache=TRUE}
debugCode("z <- function(x) scale(x)2")
```### documentCode:
This is a basic example which shows you how to document your code. Formatting style is `Roxygen2` but can be set to NULL.
```{r Example documentCode, cache=TRUE}
documentCode("z <- function(x) scale(x)^2", inLineDocumentation = NULL)
```### finishCode:
This is a basic example which shows you how to finish your code.
```{r Example finishCode, cache=TRUE}
finishCode("# A function to scale a vector and square the resulting z-score z2 <- function(")
```### optimizeCode:
This is a basic example which shows you how to optimize your code.
```{r Example optimizeCode, cache=TRUE}
optimizeCode("z <- function(x) scale(x)^2")
```### translateCode:
This is a basic example which shows you how to translate your code from `R` to `Python`.
```{r Example translateCode, cache=TRUE}
translateCode("z <- function(x) scale(x)^2", from = "R", to = "Python")
```### namingGenie:
This is a basic example which shows you how to create a function or variable name.
```{r Example namingGenie, cache=TRUE}
namingGenie("function(x) scale(x)^2")
```## Perplexity Model Arguments
The parameters of perplexity models can be adjusted by passing functional arguments.
* `text` or `code`: The text or code input required for processing by Large Language Model. If omitted, the function will utilize the content currently held in the clipboard.
* `PERPLEXITY_API_KEY`: The PERPLEXITY API key. By default it is set to `Sys.getenv("PERPLEXITY_API_KEY")`.
* `modelSelection`: Default is `"llama-3.1-sonar-small-128k-online"` but other available [models](https://docs.perplexity.ai/guides/model-cards) are:
`"llama-3.1-sonar-large-128k-online"`,
`"llama-3.1-sonar-huge-128k-online"`* `systemRole`: System role for model. Default is set to "You are a helpful assistant".
* `maxTokens`: The maximum integer of completion tokens returned by the API. The total number of tokens requested in max_tokens plus the number of prompt tokens sent in messages must not exceed the context window token limit of model requested. If left unspecified, then the model will generate tokens until either it reaches its stop token or the end of its context window.
* `temperatur`: The amount of randomness in the response, valued between 0 inclusive and 2 exclusive. Higher values are more random, and lower values are more deterministic. You should either set temperature or top_p, but not both.
* `top_p`: The nucleus sampling threshold, valued between 0 and 1 inclusive. For each subsequent token, the model considers the results of the tokens with top_p probability mass. You should either alter temperature or top_p, but not both.
* `top_k`: The number of tokens to keep for highest top-k filtering, specified as an integer between 0 and 2048 inclusive. If set to 0, top-k filtering is disabled.
* `presence_penalty`: A value between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. Incompatible with frequency_penalty.
* `frequency_penalty`: A multiplicative penalty greater than 0. Values greater than 1.0 penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. A value of 1.0 means no penalty. Incompatible with presence_penalty.
* `proxy`: Default value is NULL. To execute Perplexity queries via a proxy server, specify the proxy address and port as an argument to the function instance. Use the following format: `"proxy_address:proxy_port"`. An example would be: `"81.94.255.13:8080"`