Skip to content

Commit

Permalink
fix documentation and python test
Browse files Browse the repository at this point in the history
  • Loading branch information
CorradoLanera committed May 17, 2024
1 parent 17adb2a commit 7c8b95f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 32 deletions.
7 changes: 2 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
Package: ubep.gpt
Title: A basic/simple interface to OpenAI’s GPT API
Title: A basic and simple interface to OpenAI’s GPT API
Version: 0.2.9
Authors@R:
person("Corrado", "Lanera", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-0520-7428"))
Description: The goal of '{ubep.gpt}' is to provide a basic/simple
interface to OpenAI's GPT API. The package is designed to work with
(i.e., to query on) dataframes/tibbles, and to simplify the process of
querying the API.
Description: The goal of 'ubep.gpt' is to provide a basic and simple interface to OpenAI's GPT API (and other compatible APIs). The package is also designed to work with (i.e., to query on) dataframes/tibbles, and to simplify the process of querying the API.
License: MIT + file LICENSE
URL: https://github.com/UBESP-DCTV/ubep.gpt,
https://ubesp-dctv.github.io/ubep.gpt/
Expand Down
10 changes: 5 additions & 5 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ You can use the `query_gpt` function to query the GPT API. You can decide the mo

To use the function you need to compose a prompt. You can use (but it is not necessary!) the `compose_prompt_api` function to compose the prompt properly with an optional (single) system prompt (i.e., gpt's setup) and a (single) user prompt (i.e., the query). This function is useful because it helps you to compose the prompt automatically adopting the required API's structure.

> NOTE: you can still pass a correcly formatted list (of lists) as described in the [official documentation](https://platform.openai.com/docs/api-reference/chat) (<https://platform.openai.com/docs/api-reference/chat>).
> NOTE: you can still pass a correctly formatted list (of lists) as described in the [official documentation](https://platform.openai.com/docs/api-reference/chat) (<https://platform.openai.com/docs/api-reference/chat>).
Once you have queried the API, you can extract the content of the response using the `get_content` function. You can also extract the tokens of the prompt and the response using the `get_tokens` function.

Expand Down Expand Up @@ -255,7 +255,7 @@ cat(res) # limited to 30 tokens!

### Python's backend

Often, for complex prompt it happens that the R environment (everyone we have experiemnted, i.e. `{openai}`, `{httr}`, `{httr2}`, and `curl`) return a timeout error for a certificate validation (see, e.g.: https://github.com/irudnyts/openai/issues/61, and https://github.com/irudnyts/openai/issues/42). The same does not happen with a pure python backend usign the official OpenAI's `{openai}` library. you can setup a Python backend by executing `setup_py()`, and setting `use_py = TRUE` in the functions that send the queries (i.e., `query_gpt`, `query_gpt_on_column`, and `get_completion_from_messages`)
Often, for complex prompt it happens that the R environment (everyone we have experimented, i.e. `{openai}`, `{httr}`, `{httr2}`, and `curl`) return a timeout error for a certificate validation (see, e.g.: https://github.com/irudnyts/openai/issues/61, and https://github.com/irudnyts/openai/issues/42). The same does not happen with a pure python backend using the official OpenAI's `{openai}` library. you can setup a Python backend by executing `setup_py()`, and setting `use_py = TRUE` in the functions that send the queries (i.e., `query_gpt`, `query_gpt_on_column`, and `get_completion_from_messages`)

> NOTE: using a Python backend can be a little slower, but sometimes necessary.
Expand All @@ -274,11 +274,11 @@ cat(res)

### Personalized server's endpoint

If you have a personal server asking for queries using the OpenAI's API format, (e.g. using StudioLM, with open source models), you can set the endpoint to POST the query on your server instead of the OpenaAI one.
If you have a personal server asking for queries using the OpenAI's API format, (e.g. using LM Studio, with open source models), you can set the endpoint to POST the query on your server instead of the OpenaAI one.

> NOTE: when using personalized server endpoint, you can select the model you would like to use as usual by the `model` option. Clearly, avalilable models depend on your local server configuration.
> NOTE: when using personalized server endpoint, you can select the model you would like to use as usual by the `model` option. Clearly, available models depend on your local server configuration.
> WARNIGN: this option cannot be select if Python backend is request (i.e., setting `use_py = TRUE`, and a custom `endpoint` won't work)!
> WARNING: this option cannot be select if Python backend is request (i.e., setting `use_py = TRUE`, and a custom `endpoint` won't work)!
```{r}
if (FALSE) { # we do not run this in the README
Expand Down
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ a (single) user prompt (i.e., the query). This function is useful
because it helps you to compose the prompt automatically adopting the
required API’s structure.

> NOTE: you can still pass a correcly formatted list (of lists) as
> NOTE: you can still pass a correctly formatted list (of lists) as
> described in the [official
> documentation](https://platform.openai.com/docs/api-reference/chat)
> (<https://platform.openai.com/docs/api-reference/chat>).
Expand Down Expand Up @@ -95,40 +95,40 @@ res <- query_gpt(
)
#> ℹ Total tries: 1.
#> ℹ Prompt token used: 29.
#> ℹ Response token used: 83.
#> ℹ Total token used: 112.
#> ℹ Response token used: 100.
#> ℹ Total token used: 129.

str(res)
#> List of 7
#> $ id : chr "chatcmpl-9PpAnZZwHo5hbUew4uUzbm5wsiMG9"
#> $ id : chr "chatcmpl-9PzusvOFD57oVSnHWcp3JKk9Kr75U"
#> $ object : chr "chat.completion"
#> $ created : int 1715941937
#> $ created : int 1715983234
#> $ model : chr "gpt-3.5-turbo-0125"
#> $ choices :'data.frame': 1 obs. of 5 variables:
#> ..$ index : int 0
#> ..$ logprobs : logi NA
#> ..$ finish_reason : chr "stop"
#> ..$ finish_reason : chr "length"
#> ..$ message.role : chr "assistant"
#> ..$ message.content: chr "I supported Professor Smith with his advanced mathematics course last semester. The course covered topics such "| __truncated__
#> ..$ message.content: chr "The last course that our professor provided was on Advanced Machine Learning. This course delved into more comp"| __truncated__
#> $ usage :List of 3
#> ..$ prompt_tokens : int 29
#> ..$ completion_tokens: int 83
#> ..$ total_tokens : int 112
#> ..$ completion_tokens: int 100
#> ..$ total_tokens : int 129
#> $ system_fingerprint: NULL
get_content(res)
#> [1] "I supported Professor Smith with his advanced mathematics course last semester. The course covered topics such as linear algebra, differential equations, and complex analysis. I helped Professor Smith develop homework assignments, review materials, and conduct study sessions to assist students in understanding the challenging concepts in the course. Overall, the course was a great success, and the students were able to delve deep into these complex mathematical topics under Professor Smith's guidance."
#> [1] "The last course that our professor provided was on Advanced Machine Learning. This course delved into more complex machine learning techniques such as deep learning, reinforcement learning, and unsupervised learning. Students learned about cutting-edge algorithms and applications in areas such as natural language processing, computer vision, and recommendation systems. The course also had a significant practical component with coding assignments and a final project where students applied their knowledge to analyze real-world datasets. It was a challenging yet engaging course that provided students with valuable skills for"

# for a well formatted output on R, use `cat()`
get_content(res) |> cat()
#> I supported Professor Smith with his advanced mathematics course last semester. The course covered topics such as linear algebra, differential equations, and complex analysis. I helped Professor Smith develop homework assignments, review materials, and conduct study sessions to assist students in understanding the challenging concepts in the course. Overall, the course was a great success, and the students were able to delve deep into these complex mathematical topics under Professor Smith's guidance.
#> The last course that our professor provided was on Advanced Machine Learning. This course delved into more complex machine learning techniques such as deep learning, reinforcement learning, and unsupervised learning. Students learned about cutting-edge algorithms and applications in areas such as natural language processing, computer vision, and recommendation systems. The course also had a significant practical component with coding assignments and a final project where students applied their knowledge to analyze real-world datasets. It was a challenging yet engaging course that provided students with valuable skills for

get_tokens(res)
#> [1] 112
#> [1] 129
get_tokens(res, "prompt")
#> [1] 29
get_tokens(res, "all")
#> prompt_tokens completion_tokens total_tokens
#> 29 83 112
#> 29 100 129
```

## Easy prompt-assisted creation
Expand Down Expand Up @@ -425,11 +425,11 @@ cat(res) # limited to 30 tokens!
### Python’s backend

Often, for complex prompt it happens that the R environment (everyone we
have experiemnted, i.e. `{openai}`, `{httr}`, `{httr2}`, and `curl`)
have experimented, i.e. `{openai}`, `{httr}`, `{httr2}`, and `curl`)
return a timeout error for a certificate validation (see, e.g.:
<https://github.com/irudnyts/openai/issues/61>, and
<https://github.com/irudnyts/openai/issues/42>). The same does not
happen with a pure python backend usign the official OpenAI’s `{openai}`
happen with a pure python backend using the official OpenAI’s `{openai}`
library. you can setup a Python backend by executing `setup_py()`, and
setting `use_py = TRUE` in the functions that send the queries (i.e.,
`query_gpt`, `query_gpt_on_column`, and `get_completion_from_messages`)
Expand All @@ -448,20 +448,20 @@ res <- query_gpt(
get_content()

cat(res)
#> The last course provided by the professor was a graduate-level seminar on "Advanced Topics in Artificial Intelligence." The course covered cutting-edge research in areas such as deep learning, natural language processing, and reinforcement learning. Students were required to read and present research papers, participate in discussions, and complete a final project applying the concepts learned in the course. The professor received positive feedback from students for their engaging teaching style and ability to explain complex topics clearly.
#> The last course that the professor provided was a graduate-level seminar on "Advanced Topics in Artificial Intelligence." The course covered cutting-edge research in areas such as deep learning, natural language processing, and reinforcement learning. Students were required to read and present research papers, participate in discussions, and complete a final project applying the concepts learned in the course. The professor also invited guest speakers from industry and academia to share their expertise and insights with the students. Overall, the course was well-received by the students and provided them with valuable knowledge and skills in the field of artificial intelligence.
```

### Personalized server’s endpoint

If you have a personal server asking for queries using the OpenAI’s API
format, (e.g. using StudioLM, with open source models), you can set the
format, (e.g. using LM Studio, with open source models), you can set the
endpoint to POST the query on your server instead of the OpenaAI one.

> NOTE: when using personalized server endpoint, you can select the
> model you would like to use as usual by the `model` option. Clearly,
> avalilable models depend on your local server configuration.
> available models depend on your local server configuration.
> WARNIGN: this option cannot be select if Python backend is request
> WARNING: this option cannot be select if Python backend is request
> (i.e., setting `use_py = TRUE`, and a custom `endpoint` won’t work)!
``` r
Expand Down
7 changes: 6 additions & 1 deletion inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ Codecov
CorradoLanera
GPT
Il
LM
Lifecycle
ORCID
OpenAI
OpenAI's
OpenAI’s
OpenaAI
Questa
README
Sei
Expand Down Expand Up @@ -41,6 +43,7 @@ dell'assistente
dell'utente
delle
desiderato
deterministically
deve
di
didattica
Expand All @@ -56,6 +59,8 @@ funzione
genere
generico
gpt
gpt's
gpt’s
https
il
impostare
Expand Down Expand Up @@ -108,5 +113,5 @@ una
usare
usr
utile
venv
è
’s
4 changes: 2 additions & 2 deletions man/ubep.gpt-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/testthat/test-get_completion_from_messages.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ test_that("get_completion_from_messages works", {

test_that("get_completion_from_messages works w/ py", {
# setup
setup_py()
model <- "gpt-3.5-turbo"
messages <- compose_prompt_api(
sys_prompt = compose_sys_prompt(
Expand Down

0 comments on commit 7c8b95f

Please sign in to comment.