Closed roflmaostc closed 5 months ago
Can you try checking the global PromptingTools.OPENAI_API_KEY
?
The init is as simple as:
It's important that the ENV is set before you init the package, but you seem to have done it properly
You mean loading the package or installing it?
julia> PromptingTools.OPENAI_API_KEY
julia> ENV["OPENAI_API_KEY"]
"sk-Lasdsadsadsad"
Also fails:
julia> ENV["OPENAI_API_KEY"] = "sk-aasdasdasdasdasd"
"sk-asdasdasdasd
(@v1.10) pkg> add Prom
PromClient
Prometheus
PromptingTools
(@v1.10) pkg> add PromptingTools
Resolving package versions...
Updating `~/.julia/environments/v1.10/Project.toml`
[670122d1] + PromptingTools v0.9.0
Updating `~/.julia/environments/v1.10/Manifest.toml`
[d1d4a3ce] + BitFlags v0.1.8
[944b1d66] + CodecZlib v0.7.3
[f0e56b4a] + ConcurrentUtilities v2.3.0
[460bff9d] + ExceptionUnwrapping v0.1.10
[cd3eb016] + HTTP v1.10.1
[0f8b85d8] + JSON3 v1.14.0
[e6f89c97] + LoggingExtras v1.0.3
[739be429] + MbedTLS v1.1.9
⌅ [e9f21f70] + OpenAI v0.8.7
[4d8831e6] + OpenSSL v1.4.1
[670122d1] + PromptingTools v0.9.0
[777ac1f9] + SimpleBufferStream v1.1.0
[856f2bd8] + StructTypes v1.10.0
[3bb67fe8] + TranscodingStreams v0.10.2
[5c2747f8] + URIs v1.5.1
[458c3c95] + OpenSSL_jll v3.0.12+0
[8dfed614] + Test
Info Packages marked with ⌅ have new versions available but compatibility constraints restrict them from upgrading. To see why use `status --outdated -m`
julia> using PromptingTools
julia> PromptingTools.OPENAI_API_KEY
""
So might be failing since I'm on the non-paid version of OpenAI?
Sorry, when loading the package.
For whatever reason that ENV variable wasn't available when the package was loaded. Can you try again? There isn't much I can do without access to your system. Could you restart REPL, double-check that the ENV exists, and then load the PT package?
As a workaround, set the API key directly: PromptingTools.OPENAI_API_KEY=ENV["OPENAI_API_KEY"]
So might be failing since I'm on the non-paid version of OpenAI?
I think that would give you an authorization error from the OpenAI server. This error is caught because you're providing an empty string
That gave indeed an OPENAI error. But still not clear why the workflow failed before. Using Julia 1.10 and Linux. But since I did everything inside the REPL, should not matter.
julia> PromptingTools.OPENAI_API_KEY="sk-asdasdasd"
WARNING: redefinition of constant PromptingTools.OPENAI_API_KEY. This may fail, cause incorrect answers, or produce other errors.
"sk-asdasdasd"
julia> ai"lol"
ERROR: HTTP.Exceptions.StatusError(429, "POST", "/v1/chat/completions", HTTP.Messages.Response:
"""
HTTP/1.1 429 Too Many Requests
Date: Tue, 23 Jan 2024 09:50:42 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 337
Connection: keep-alive
vary: Origin
CF-Cache-Status: DYNAMIC
Set-Cookie: ******
Set-Cookie: ******
Server: cloudflare
alt-svc: h3=":443"; ma=86400
{
"error": {
"message": "You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.",
"type": "insufficient_quota",
"param": null,
"code": "insufficient_quota"
}
}
""")
Stacktrace:
[1] (::HTTP.ConnectionRequest.var"#connections#4"{…})(req::HTTP.Messages.Request; proxy::Nothing, socket_type::Type, socket_type_tls::Type, readtimeout::Int64, connect_timeout::Int64, logerrors::Bool, logtag::Nothing, kw::@Kwargs{…})
@ HTTP.ConnectionRequest ~/.julia/packages/HTTP/bDoga/src/clientlayers/ConnectionRequest.jl:144
[2] (::Base.var"#96#98"{…})(args::HTTP.Messages.Request; kwargs::@Kwargs{…})
@ Base ./error.jl:308
[3] (::HTTP.RetryRequest.var"#manageretries#3"{…})(req::HTTP.Messages.Request; retry::Bool, retries::Int64, retry_delays::ExponentialBackOff, retry_check::Function, retry_non_idempotent::Bool, kw::@Kwargs{…})
@ HTTP.RetryRequest ~/.julia/packages/HTTP/bDoga/src/clientlayers/RetryRequest.jl:75
[4] manageretries
@ ~/.julia/packages/HTTP/bDoga/src/clientlayers/RetryRequest.jl:30 [inlined]
[5] (::HTTP.CookieRequest.var"#managecookies#4"{…})(req::HTTP.Messages.Request; cookies::Bool, cookiejar::HTTP.Cookies.CookieJar, kw::@Kwargs{…})
@ HTTP.CookieRequest ~/.julia/packages/HTTP/bDoga/src/clientlayers/CookieRequest.jl:42
[6] managecookies
@ ~/.julia/packages/HTTP/bDoga/src/clientlayers/CookieRequest.jl:19 [inlined]
[7] (::HTTP.HeadersRequest.var"#defaultheaders#2"{…})(req::HTTP.Messages.Request; iofunction::Nothing, decompress::Nothing, basicauth::Bool, detect_content_type::Bool, canonicalize_headers::Bool, kw::@Kwargs{…})
@ HTTP.HeadersRequest ~/.julia/packages/HTTP/bDoga/src/clientlayers/HeadersRequest.jl:71
[8] defaultheaders
@ ~/.julia/packages/HTTP/bDoga/src/clientlayers/HeadersRequest.jl:14 [inlined]
[9] (::HTTP.RedirectRequest.var"#redirects#3"{…})(req::HTTP.Messages.Request; redirect::Bool, redirect_limit::Int64, redirect_method::Nothing, forwardheaders::Bool, response_stream::Nothing, kw::@Kwargs{…})
@ HTTP.RedirectRequest ~/.julia/packages/HTTP/bDoga/src/clientlayers/RedirectRequest.jl:25
[10] redirects
@ ~/.julia/packages/HTTP/bDoga/src/clientlayers/RedirectRequest.jl:14 [inlined]
[11] (::HTTP.MessageRequest.var"#makerequest#3"{…})(method::String, url::URIs.URI, headers::Vector{…}, body::IOBuffer; copyheaders::Bool, response_stream::Nothing, http_version::HTTP.Strings.HTTPVersion, verbose::Int64, kw::@Kwargs{…})
@ HTTP.MessageRequest ~/.julia/packages/HTTP/bDoga/src/clientlayers/MessageRequest.jl:35
[12] makerequest
@ ~/.julia/packages/HTTP/bDoga/src/clientlayers/MessageRequest.jl:24 [inlined]
[13] request(stack::HTTP.MessageRequest.var"#makerequest#3"{…}, method::String, url::String, h::Vector{…}, b::IOBuffer, q::Nothing; headers::Vector{…}, body::IOBuffer, query::Nothing, kw::@Kwargs{…})
@ HTTP ~/.julia/packages/HTTP/bDoga/src/HTTP.jl:457
[14] #request#20
@ ~/.julia/packages/HTTP/bDoga/src/HTTP.jl:315 [inlined]
[15] #request_body#3
@ ~/.julia/packages/OpenAI/wLQsT/src/OpenAI.jl:80 [inlined]
[16] request_body
@ ~/.julia/packages/OpenAI/wLQsT/src/OpenAI.jl:78 [inlined]
[17] _request(api::String, provider::OpenAI.OpenAIProvider, api_key::String; method::String, http_kwargs::@NamedTuple{…}, streamcallback::Nothing, kwargs::@Kwargs{…})
@ OpenAI ~/.julia/packages/OpenAI/wLQsT/src/OpenAI.jl:145
[18] _request
@ ~/.julia/packages/OpenAI/wLQsT/src/OpenAI.jl:135 [inlined]
[19] #openai_request#14
@ ~/.julia/packages/OpenAI/wLQsT/src/OpenAI.jl:173 [inlined]
[20] openai_request
@ ~/.julia/packages/OpenAI/wLQsT/src/OpenAI.jl:171 [inlined]
[21] #create_chat#19
@ ~/.julia/packages/OpenAI/wLQsT/src/OpenAI.jl:307 [inlined]
[22] create_chat
@ ~/.julia/packages/OpenAI/wLQsT/src/OpenAI.jl:306 [inlined]
[23] #create_chat#134
@ ~/.julia/packages/PromptingTools/tzE4w/src/llm_openai.jl:85 [inlined]
[24] create_chat
@ ~/.julia/packages/PromptingTools/tzE4w/src/llm_openai.jl:80 [inlined]
[25] macro expansion
@ ./timing.jl:395 [inlined]
[26] aigenerate(prompt_schema::PromptingTools.OpenAISchema, prompt::String; verbose::Bool, api_key::String, model::String, return_all::Bool, dry_run::Bool, conversation::Vector{…}, http_kwargs::@NamedTuple{…}, api_kwargs::@NamedTuple{}, kwargs::@Kwargs{})
@ PromptingTools ~/.julia/packages/PromptingTools/tzE4w/src/llm_openai.jl:327
[27] aigenerate(prompt::String; model::String, kwargs::@Kwargs{return_all::Bool})
@ PromptingTools ~/.julia/packages/PromptingTools/tzE4w/src/llm_interface.jl:208
[28] macro expansion
@ ~/.julia/packages/PromptingTools/tzE4w/src/macros.jl:39 [inlined]
[29] top-level scope
@ REPL[9]:1
caused by: HTTP.Exceptions.StatusError(429, "POST", "/v1/chat/completions", HTTP.Messages.Response:
"""
HTTP/1.1 429 Too Many Requests
Date: Tue, 23 Jan 2024 09:50:42 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 337
Connection: keep-alive
vary: Origin
strict-transport-security: max-age=15724800; includeSubDomains
CF-Cache-Status: DYNAMIC
Set-Cookie: ******
Set-Cookie: ******
Server: cloudfl
alt-svc: h3=":443"; ma=86400
{
"error": {
"message": "You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.",
"type": "insufficient_quota",
"param": null,
"code": "insufficient_quota"
}
}
""")
Stacktrace:
[1] (::HTTP.ExceptionRequest.var"#exceptions#2"{…})(stream::HTTP.Streams.Stream{…}; status_exception::Bool, timedout::ConcurrentUtilities.TimedOut{…}, logerrors::Bool, logtag::Nothing, kw::@Kwargs{…})
@ HTTP.ExceptionRequest ~/.julia/packages/HTTP/bDoga/src/clientlayers/ExceptionRequest.jl:19
[2] exceptions
@ ~/.julia/packages/HTTP/bDoga/src/clientlayers/ExceptionRequest.jl:13 [inlined]
[3] #2
@ ~/.julia/packages/HTTP/bDoga/src/clientlayers/TimeoutRequest.jl:22 [inlined]
[4] (::ConcurrentUtilities.var"#2#4"{HTTP.Messages.Response, Timer, ConcurrentUtilities.TimedOut{…}, Channel{…}, HTTP.TimeoutRequest.var"#2#5"{…}})()
@ ConcurrentUtilities ~/.julia/packages/ConcurrentUtilities/dDnHe/src/try_with_timeout.jl:82
Stacktrace:
[1] try_yieldto(undo::typeof(Base.ensure_rescheduled))
@ Base ./task.jl:931
[2] wait()
@ Base ./task.jl:995
[3] wait(c::Base.GenericCondition{ReentrantLock}; first::Bool)
@ Base ./condition.jl:130
[4] wait
@ ./condition.jl:125 [inlined]
[5] take_unbuffered(c::Channel{HTTP.Messages.Response})
@ Base ./channels.jl:494
[6] take!
@ ./channels.jl:471 [inlined]
[7] try_with_timeout(f::Function, timeout::Int64, ::Type{HTTP.Messages.Response})
@ ConcurrentUtilities ~/.julia/packages/ConcurrentUtilities/dDnHe/src/try_with_timeout.jl:89
[8] (::HTTP.TimeoutRequest.var"#timeouts#3"{…})(stream::HTTP.Streams.Stream{…}; readtimeout::Int64, logerrors::Bool, logtag::Nothing, kw::@Kwargs{…})
@ HTTP.TimeoutRequest ~/.julia/packages/HTTP/bDoga/src/clientlayers/TimeoutRequest.jl:21
[9] (::HTTP.ConnectionRequest.var"#connections#4"{…})(req::HTTP.Messages.Request; proxy::Nothing, socket_type::Type, socket_type_tls::Type, readtimeout::Int64, connect_timeout::Int64, logerrors::Bool, logtag::Nothing, kw::@Kwargs{…})
@ HTTP.ConnectionRequest ~/.julia/packages/HTTP/bDoga/src/clientlayers/ConnectionRequest.jl:120
[10] (::Base.var"#96#98"{…})(args::HTTP.Messages.Request; kwargs::@Kwargs{…})
@ Base ./error.jl:308
[11] (::HTTP.RetryRequest.var"#manageretries#3"{…})(req::HTTP.Messages.Request; retry::Bool, retries::Int64, retry_delays::ExponentialBackOff, retry_check::Function, retry_non_idempotent::Bool, kw::@Kwargs{…})
@ HTTP.RetryRequest ~/.julia/packages/HTTP/bDoga/src/clientlayers/RetryRequest.jl:75
[12] manageretries
@ ~/.julia/packages/HTTP/bDoga/src/clientlayers/RetryRequest.jl:30 [inlined]
[13] (::HTTP.CookieRequest.var"#managecookies#4"{…})(req::HTTP.Messages.Request; cookies::Bool, cookiejar::HTTP.Cookies.CookieJar, kw::@Kwargs{…})
@ HTTP.CookieRequest ~/.julia/packages/HTTP/bDoga/src/clientlayers/CookieRequest.jl:42
[14] managecookies
@ ~/.julia/packages/HTTP/bDoga/src/clientlayers/CookieRequest.jl:19 [inlined]
[15] (::HTTP.HeadersRequest.var"#defaultheaders#2"{…})(req::HTTP.Messages.Request; iofunction::Nothing, decompress::Nothing, basicauth::Bool, detect_content_type::Bool, canonicalize_headers::Bool, kw::@Kwargs{…})
@ HTTP.HeadersRequest ~/.julia/packages/HTTP/bDoga/src/clientlayers/HeadersRequest.jl:71
[16] defaultheaders
@ ~/.julia/packages/HTTP/bDoga/src/clientlayers/HeadersRequest.jl:14 [inlined]
[17] (::HTTP.RedirectRequest.var"#redirects#3"{…})(req::HTTP.Messages.Request; redirect::Bool, redirect_limit::Int64, redirect_method::Nothing, forwardheaders::Bool, response_stream::Nothing, kw::@Kwargs{…})
@ HTTP.RedirectRequest ~/.julia/packages/HTTP/bDoga/src/clientlayers/RedirectRequest.jl:25
[18] redirects
@ ~/.julia/packages/HTTP/bDoga/src/clientlayers/RedirectRequest.jl:14 [inlined]
[19] (::HTTP.MessageRequest.var"#makerequest#3"{…})(method::String, url::URIs.URI, headers::Vector{…}, body::IOBuffer; copyheaders::Bool, response_stream::Nothing, http_version::HTTP.Strings.HTTPVersion, verbose::Int64, kw::@Kwargs{…})
@ HTTP.MessageRequest ~/.julia/packages/HTTP/bDoga/src/clientlayers/MessageRequest.jl:35
[20] makerequest
@ ~/.julia/packages/HTTP/bDoga/src/clientlayers/MessageRequest.jl:24 [inlined]
[21] request(stack::HTTP.MessageRequest.var"#makerequest#3"{…}, method::String, url::String, h::Vector{…}, b::IOBuffer, q::Nothing; headers::Vector{…}, body::IOBuffer, query::Nothing, kw::@Kwargs{…})
@ HTTP ~/.julia/packages/HTTP/bDoga/src/HTTP.jl:457
[22] #request#20
@ ~/.julia/packages/HTTP/bDoga/src/HTTP.jl:315 [inlined]
[23] #request_body#3
@ ~/.julia/packages/OpenAI/wLQsT/src/OpenAI.jl:80 [inlined]
[24] request_body
@ ~/.julia/packages/OpenAI/wLQsT/src/OpenAI.jl:78 [inlined]
[25] _request(api::String, provider::OpenAI.OpenAIProvider, api_key::String; method::String, http_kwargs::@NamedTuple{…}, streamcallback::Nothing, kwargs::@Kwargs{…})
@ OpenAI ~/.julia/packages/OpenAI/wLQsT/src/OpenAI.jl:145
[26] _request
@ ~/.julia/packages/OpenAI/wLQsT/src/OpenAI.jl:135 [inlined]
[27] #openai_request#14
@ ~/.julia/packages/OpenAI/wLQsT/src/OpenAI.jl:173 [inlined]
[28] openai_request
@ ~/.julia/packages/OpenAI/wLQsT/src/OpenAI.jl:171 [inlined]
[29] #create_chat#19
@ ~/.julia/packages/OpenAI/wLQsT/src/OpenAI.jl:307 [inlined]
[30] create_chat
@ ~/.julia/packages/OpenAI/wLQsT/src/OpenAI.jl:306 [inlined]
[31] #create_chat#134
@ ~/.julia/packages/PromptingTools/tzE4w/src/llm_openai.jl:85 [inlined]
[32] create_chat
@ ~/.julia/packages/PromptingTools/tzE4w/src/llm_openai.jl:80 [inlined]
[33] macro expansion
@ ./timing.jl:395 [inlined]
[34] aigenerate(prompt_schema::PromptingTools.OpenAISchema, prompt::String; verbose::Bool, api_key::String, model::String, return_all::Bool, dry_run::Bool, conversation::Vector{…}, http_kwargs::@NamedTuple{…}, api_kwargs::@NamedTuple{}, kwargs::@Kwargs{})
@ PromptingTools ~/.julia/packages/PromptingTools/tzE4w/src/llm_openai.jl:327
[35] aigenerate(prompt::String; model::String, kwargs::@Kwargs{return_all::Bool})
@ PromptingTools ~/.julia/packages/PromptingTools/tzE4w/src/llm_interface.jl:208
[36] macro expansion
@ ~/.julia/packages/PromptingTools/tzE4w/src/macros.jl:39 [inlined]
[37] top-level scope
@ REPL[9]:1
Some type information was truncated. Use `show(err)` to see complete types.
Hmm, I have no idea. I get the ENV key here. I don't see how that would fail if you run in a fresh session with ENV set...
I've just tried:
(@v1.10) pkg> activate --temp
Activating new project at `/var/folders/0m/sbjycxt54_q5656v9r77p23m0000gs/T/jl_7qQj7H`
(jl_7qQj7H) julia> ENV["OPENAI_API_KEY"]="xxx"
"xxx"
(jl_7qQj7H) julia> ENV["OPENAI_API_KEY"]
"xxx"
(jl_7qQj7H) julia> using PromptingTools
No Changes to `/private/var/folders/0m/sbjycxt54_q5656v9r77p23m0000gs/T/jl_7qQj7H/Project.toml`
No Changes to `/private/var/folders/0m/sbjycxt54_q5656v9r77p23m0000gs/T/jl_7qQj7H/Manifest.toml`
[ Info: Precompiling PromptingTools [670122d1-24a8-4d70-bfce-740807c42192]
[ Info: Precompiling RAGToolsExperimentalExt [c8faa96b-7971-5b29-a9f8-45b2946b3ca9]
(jl_7qQj7H) julia> PromptingTools.OPENAI_API_KEY
"xxx"
And it loads the ENV I provided. I'm out of ideas :(
As an alternative, you can always try the Preferences.jl route: PromptingTools.set_preferences!("OPENAI_API_KEY"=>"abc")
(careful about syncing your API key in the LocalPreferences.toml)
As discussed on Slack, this seems to happen in some weird interaction with precompilation+inlining (if you loaded the package the first time without the OpenAI API key, it inlines the resulting value at the time...)
The solution seems to be to remove the precompilation cache of this package, eg,
.julia/compiled/v1.10/PromptingTools
Afterwards, assuming your ENV is set BEFORE your load the package, all should work as expected.
@roflmaostc has removing the compiled cache resolved your issue?
Yes, seems like.
But still a weird workaround, right? Is there no way to avoid this precompilation? How are other packages do this?
Yes, seems like.
But still a weird workaround, right? Is there no way to avoid this precompilation? How are other packages do this?
Agreed. I think it's somewhere between a feature and a bug coming for the compiler. It shouldn't be inlining a getter for ENV that can change across runs.
I've merged a fix with explicit @noinline
annotation.
I just created a fresh OpenAI key. Is there some time lag between creating and using them?
At least this fails for me;