-
Notifications
You must be signed in to change notification settings - Fork 8
/
llm_openai.jl
1470 lines (1318 loc) · 61.6 KB
/
llm_openai.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## Rendering of converation history for the OpenAI API
"""
render(schema::AbstractOpenAISchema,
messages::Vector{<:AbstractMessage};
image_detail::AbstractString = "auto",
conversation::AbstractVector{<:AbstractMessage} = AbstractMessage[],
kwargs...)
Builds a history of the conversation to provide the prompt to the API. All unspecified kwargs are passed as replacements such that `{{key}}=>value` in the template.
# Keyword Arguments
- `image_detail`: Only for `UserMessageWithImages`. It represents the level of detail to include for images. Can be `"auto"`, `"high"`, or `"low"`.
- `conversation`: An optional vector of `AbstractMessage` objects representing the conversation history. If not provided, it is initialized as an empty vector.
"""
function render(schema::AbstractOpenAISchema,
messages::Vector{<:AbstractMessage};
image_detail::AbstractString = "auto",
conversation::AbstractVector{<:AbstractMessage} = AbstractMessage[],
kwargs...)
##
@assert image_detail in ["auto", "high", "low"] "Image detail must be one of: auto, high, low"
## First pass: keep the message types but make the replacements provided in `kwargs`
messages_replaced = render(NoSchema(), messages; conversation, kwargs...)
## Second pass: convert to the OpenAI schema
conversation = Dict{String, Any}[]
# replace any handlebar variables in the messages
for msg in messages_replaced
## Special case for images
if msg isa UserMessageWithImages
# Build message content
content = Dict{String, Any}[Dict("type" => "text",
"text" => msg.content)]
# Add images
for img in msg.image_url
push!(content,
Dict("type" => "image_url",
"image_url" => Dict("url" => img,
"detail" => image_detail)))
end
else
content = msg.content
end
push!(conversation, Dict("role" => role4render(schema, msg), "content" => content))
end
return conversation
end
## OpenAI.jl back-end
## Types
# "Providers" are a way to use other APIs that are compatible with OpenAI API specs, eg, Azure and mamy more
# Define our sub-type to distinguish it from other OpenAI.jl providers
abstract type AbstractCustomProvider <: OpenAI.AbstractOpenAIProvider end
Base.@kwdef struct CustomProvider <: AbstractCustomProvider
api_key::String = ""
base_url::String = "http://localhost:8080"
api_version::String = ""
end
function OpenAI.build_url(provider::AbstractCustomProvider, api::AbstractString)
string(provider.base_url, "/", api)
end
function OpenAI.auth_header(provider::AbstractCustomProvider, api_key::AbstractString)
OpenAI.auth_header(
OpenAI.OpenAIProvider(provider.api_key,
provider.base_url,
provider.api_version),
api_key)
end
## Extend OpenAI create_chat to allow for testing/debugging
# Default passthrough
function OpenAI.create_chat(schema::AbstractOpenAISchema,
api_key::AbstractString,
model::AbstractString,
conversation;
kwargs...)
OpenAI.create_chat(api_key, model, conversation; kwargs...)
end
# Overload for testing/debugging
function OpenAI.create_chat(schema::TestEchoOpenAISchema, api_key::AbstractString,
model::AbstractString,
conversation; kwargs...)
schema.model_id = model
schema.inputs = conversation
return schema
end
"""
OpenAI.create_chat(schema::CustomOpenAISchema,
api_key::AbstractString,
model::AbstractString,
conversation;
url::String="http://localhost:8080",
kwargs...)
Dispatch to the OpenAI.create_chat function, for any OpenAI-compatible API.
It expects `url` keyword argument. Provide it to the `aigenerate` function via `api_kwargs=(; url="my-url")`
It will forward your query to the "chat/completions" endpoint of the base URL that you provided (=`url`).
"""
function OpenAI.create_chat(schema::CustomOpenAISchema,
api_key::AbstractString,
model::AbstractString,
conversation;
url::String = "http://localhost:8080",
kwargs...)
# Build the corresponding provider object
# Create chat will automatically pass our data to endpoint `/chat/completions`
provider = CustomProvider(; api_key, base_url = url)
OpenAI.create_chat(provider, model, conversation; kwargs...)
end
"""
OpenAI.create_chat(schema::LocalServerOpenAISchema,
api_key::AbstractString,
model::AbstractString,
conversation;
url::String = "http://localhost:8080",
kwargs...)
Dispatch to the OpenAI.create_chat function, but with the LocalServer API parameters, ie, defaults to `url` specified by the `LOCAL_SERVER` preference. See `?PREFERENCES`
"""
function OpenAI.create_chat(schema::LocalServerOpenAISchema,
api_key::AbstractString,
model::AbstractString,
conversation;
url::String = LOCAL_SERVER,
kwargs...)
OpenAI.create_chat(CustomOpenAISchema(), api_key, model, conversation; url, kwargs...)
end
"""
OpenAI.create_chat(schema::MistralOpenAISchema,
api_key::AbstractString,
model::AbstractString,
conversation;
url::String="https://api.mistral.ai/v1",
kwargs...)
Dispatch to the OpenAI.create_chat function, but with the MistralAI API parameters.
It tries to access the `MISTRALAI_API_KEY` ENV variable, but you can also provide it via the `api_key` keyword argument.
"""
function OpenAI.create_chat(schema::MistralOpenAISchema,
api_key::AbstractString,
model::AbstractString,
conversation;
url::String = "https://api.mistral.ai/v1",
kwargs...)
# Build the corresponding provider object
# try to override provided api_key because the default is OpenAI key
provider = CustomProvider(;
api_key = isempty(MISTRALAI_API_KEY) ? api_key : MISTRALAI_API_KEY,
base_url = url)
OpenAI.create_chat(provider, model, conversation; kwargs...)
end
function OpenAI.create_chat(schema::FireworksOpenAISchema,
api_key::AbstractString,
model::AbstractString,
conversation;
url::String = "https://api.fireworks.ai/inference/v1",
kwargs...)
# Build the corresponding provider object
# try to override provided api_key because the default is OpenAI key
provider = CustomProvider(;
api_key = isempty(FIREWORKS_API_KEY) ? api_key : FIREWORKS_API_KEY,
base_url = url)
OpenAI.create_chat(provider, model, conversation; kwargs...)
end
function OpenAI.create_chat(schema::TogetherOpenAISchema,
api_key::AbstractString,
model::AbstractString,
conversation;
url::String = "https://api.together.xyz/v1",
kwargs...)
# Build the corresponding provider object
# try to override provided api_key because the default is OpenAI key
provider = CustomProvider(;
api_key = isempty(TOGETHER_API_KEY) ? api_key : TOGETHER_API_KEY,
base_url = url)
OpenAI.create_chat(provider, model, conversation; kwargs...)
end
function OpenAI.create_chat(schema::GroqOpenAISchema,
api_key::AbstractString,
model::AbstractString,
conversation;
url::String = "https://api.groq.com/openai/v1",
kwargs...)
# Build the corresponding provider object
# try to override provided api_key because the default is OpenAI key
provider = CustomProvider(;
api_key = isempty(GROQ_API_KEY) ? api_key : GROQ_API_KEY,
base_url = url)
OpenAI.create_chat(provider, model, conversation; kwargs...)
end
function OpenAI.create_chat(schema::DeepSeekOpenAISchema,
api_key::AbstractString,
model::AbstractString,
conversation;
url::String = "https://api.deepseek.com/v1",
kwargs...)
# Build the corresponding provider object
# try to override provided api_key because the default is OpenAI key
provider = CustomProvider(;
api_key = isempty(DEEPSEEK_API_KEY) ? api_key : DEEPSEEK_API_KEY,
base_url = url)
OpenAI.create_chat(provider, model, conversation; kwargs...)
end
function OpenAI.create_chat(schema::DatabricksOpenAISchema,
api_key::AbstractString,
model::AbstractString,
conversation;
url::String = "https://<workspace_host>.databricks.com",
kwargs...)
# Build the corresponding provider object
provider = CustomProvider(;
api_key = isempty(DATABRICKS_API_KEY) ? api_key : DATABRICKS_API_KEY,
base_url = isempty(DATABRICKS_HOST) ? url : DATABRICKS_HOST)
# Override standard OpenAI request endpoint
OpenAI.openai_request("serving-endpoints/$model/invocations",
provider;
method = "POST",
model,
messages = conversation,
kwargs...)
end
# Extend OpenAI create_embeddings to allow for testing
function OpenAI.create_embeddings(schema::AbstractOpenAISchema,
api_key::AbstractString,
docs,
model::AbstractString;
kwargs...)
OpenAI.create_embeddings(api_key, docs, model; kwargs...)
end
function OpenAI.create_embeddings(schema::TestEchoOpenAISchema, api_key::AbstractString,
docs,
model::AbstractString; kwargs...)
schema.model_id = model
schema.inputs = docs
return schema
end
function OpenAI.create_embeddings(schema::CustomOpenAISchema,
api_key::AbstractString,
docs,
model::AbstractString;
url::String = "http://localhost:8080",
kwargs...)
# Build the corresponding provider object
# Create chat will automatically pass our data to endpoint `/embeddings`
provider = CustomProvider(; api_key, base_url = url)
OpenAI.create_embeddings(provider, docs, model; kwargs...)
end
# Set url and just forward to CustomOpenAISchema otherwise
# Note: Llama.cpp and hence Llama.jl DO NOT support the embeddings endpoint !! (they use `/embedding`)
function OpenAI.create_embeddings(schema::LocalServerOpenAISchema,
api_key::AbstractString,
docs,
model::AbstractString;
## Strip the "v1" from the end of the url
url::String = LOCAL_SERVER,
kwargs...)
OpenAI.create_embeddings(CustomOpenAISchema(),
api_key,
docs,
model;
url,
kwargs...)
end
function OpenAI.create_embeddings(schema::MistralOpenAISchema,
api_key::AbstractString,
docs,
model::AbstractString;
url::String = "https://api.mistral.ai/v1",
kwargs...)
# Build the corresponding provider object
# try to override provided api_key because the default is OpenAI key
provider = CustomProvider(;
api_key = isempty(MISTRALAI_API_KEY) ? api_key : MISTRALAI_API_KEY,
base_url = url)
OpenAI.create_embeddings(provider, docs, model; kwargs...)
end
function OpenAI.create_embeddings(schema::DatabricksOpenAISchema,
api_key::AbstractString,
docs,
model::AbstractString;
url::String = "https://<workspace_host>.databricks.com",
kwargs...)
# Build the corresponding provider object
provider = CustomProvider(;
api_key = isempty(DATABRICKS_API_KEY) ? api_key : DATABRICKS_API_KEY,
base_url = isempty(DATABRICKS_HOST) ? url : DATABRICKS_HOST)
# Override standard OpenAI request endpoint
OpenAI.openai_request("serving-endpoints/$model/invocations",
provider;
method = "POST",
model,
input = docs,
kwargs...)
end
function OpenAI.create_embeddings(schema::TogetherOpenAISchema,
api_key::AbstractString,
docs,
model::AbstractString;
url::String = "https://api.together.xyz/v1",
kwargs...)
provider = CustomProvider(;
api_key = isempty(TOGETHER_API_KEY) ? api_key : TOGETHER_API_KEY,
base_url = url)
OpenAI.create_embeddings(provider, docs, model; kwargs...)
end
function OpenAI.create_embeddings(schema::FireworksOpenAISchema,
api_key::AbstractString,
docs,
model::AbstractString;
url::String = "https://api.fireworks.ai/inference/v1",
kwargs...)
provider = CustomProvider(;
api_key = isempty(FIREWORKS_API_KEY) ? api_key : FIREWORKS_API_KEY,
base_url = url)
OpenAI.create_embeddings(provider, docs, model; kwargs...)
end
## Temporary fix -- it will be moved upstream
function OpenAI.create_embeddings(provider::AbstractCustomProvider,
input,
model_id::String = OpenAI.DEFAULT_EMBEDDING_MODEL_ID;
http_kwargs::NamedTuple = NamedTuple(),
kwargs...)
return OpenAI.openai_request("embeddings",
provider;
method = "POST",
http_kwargs = http_kwargs,
model = model_id,
input,
kwargs...)
end
## Wrap create_images for testing and routing
## Note: Careful, API is non-standard compared to other OAI functions
function OpenAI.create_images(schema::AbstractOpenAISchema,
api_key::AbstractString,
prompt,
args...;
kwargs...)
OpenAI.create_images(api_key, prompt, args...; kwargs...)
end
function OpenAI.create_images(schema::TestEchoOpenAISchema,
api_key::AbstractString,
prompt,
args...;
kwargs...)
schema.model_id = get(kwargs, :model, "")
schema.inputs = prompt
return schema
end
"""
response_to_message(schema::AbstractOpenAISchema,
MSG::Type{AIMessage},
choice,
resp;
model_id::AbstractString = "",
time::Float64 = 0.0,
run_id::Integer = rand(Int16),
sample_id::Union{Nothing, Integer} = nothing)
Utility to facilitate unwrapping of HTTP response to a message type `MSG` provided for OpenAI-like responses
Note: Extracts `finish_reason` and `log_prob` if available in the response.
# Arguments
- `schema::AbstractOpenAISchema`: The schema for the prompt.
- `MSG::Type{AIMessage}`: The message type to be returned.
- `choice`: The choice from the response (eg, one of the completions).
- `resp`: The response from the OpenAI API.
- `model_id::AbstractString`: The model ID to use for generating the response. Defaults to an empty string.
- `time::Float64`: The elapsed time for the response. Defaults to `0.0`.
- `run_id::Integer`: The run ID for the response. Defaults to a random integer.
- `sample_id::Union{Nothing, Integer}`: The sample ID for the response (if there are multiple completions). Defaults to `nothing`.
"""
function response_to_message(schema::AbstractOpenAISchema,
MSG::Type{AIMessage},
choice,
resp;
model_id::AbstractString = "",
time::Float64 = 0.0,
run_id::Int = Int(rand(Int32)),
sample_id::Union{Nothing, Integer} = nothing)
## extract sum log probability
has_log_prob = haskey(choice, :logprobs) &&
!isnothing(get(choice, :logprobs, nothing)) &&
haskey(choice[:logprobs], :content) &&
!isnothing(choice[:logprobs][:content])
log_prob = if has_log_prob
sum([get(c, :logprob, 0.0) for c in choice[:logprobs][:content]])
else
nothing
end
## calculate cost
tokens_prompt = get(resp.response, :usage, Dict(:prompt_tokens => 0))[:prompt_tokens]
tokens_completion = get(resp.response, :usage, Dict(:completion_tokens => 0))[:completion_tokens]
cost = call_cost(tokens_prompt, tokens_completion, model_id)
## build AIMessage object
msg = MSG(;
content = choice[:message][:content] |> strip,
status = Int(resp.status),
cost,
run_id,
sample_id,
log_prob,
finish_reason = get(choice, :finish_reason, nothing),
tokens = (tokens_prompt,
tokens_completion),
elapsed = time)
end
## User-Facing API
"""
aigenerate(prompt_schema::AbstractOpenAISchema, prompt::ALLOWED_PROMPT_TYPE;
verbose::Bool = true,
api_key::String = OPENAI_API_KEY,
model::String = MODEL_CHAT, return_all::Bool = false, dry_run::Bool = false,
http_kwargs::NamedTuple = (retry_non_idempotent = true,
retries = 5,
readtimeout = 120), api_kwargs::NamedTuple = NamedTuple(),
kwargs...)
Generate an AI response based on a given prompt using the OpenAI API.
# Arguments
- `prompt_schema`: An optional object to specify which prompt template should be applied (Default to `PROMPT_SCHEMA = OpenAISchema`)
- `prompt`: Can be a string representing the prompt for the AI conversation, a `UserMessage`, a vector of `AbstractMessage` or an `AITemplate`
- `verbose`: A boolean indicating whether to print additional information.
- `api_key`: A string representing the API key for accessing the OpenAI API.
- `model`: A string representing the model to use for generating the response. Can be an alias corresponding to a model ID defined in `MODEL_ALIASES`.
- `return_all::Bool=false`: If `true`, returns the entire conversation history, otherwise returns only the last message (the `AIMessage`).
- `dry_run::Bool=false`: If `true`, skips sending the messages to the model (for debugging, often used with `return_all=true`).
- `conversation`: An optional vector of `AbstractMessage` objects representing the conversation history. If not provided, it is initialized as an empty vector.
- `http_kwargs`: A named tuple of HTTP keyword arguments.
- `api_kwargs`: A named tuple of API keyword arguments. Useful parameters include:
- `temperature`: A float representing the temperature for sampling (ie, the amount of "creativity"). Often defaults to `0.7`.
- `logprobs`: A boolean indicating whether to return log probabilities for each token. Defaults to `false`.
- `n`: An integer representing the number of completions to generate at once (if supported).
- `stop`: A vector of strings representing the stop conditions for the conversation. Defaults to an empty vector.
- `kwargs`: Prompt variables to be used to fill the prompt/template
# Returns
If `return_all=false` (default):
- `msg`: An `AIMessage` object representing the generated AI message, including the content, status, tokens, and elapsed time.
Use `msg.content` to access the extracted string.
If `return_all=true`:
- `conversation`: A vector of `AbstractMessage` objects representing the conversation history, including the response from the AI model (`AIMessage`).
See also: `ai_str`, `aai_str`, `aiembed`, `aiclassify`, `aiextract`, `aiscan`, `aitemplates`
# Example
Simple hello world to test the API:
```julia
result = aigenerate("Say Hi!")
# [ Info: Tokens: 29 @ Cost: \$0.0 in 1.0 seconds
# AIMessage("Hello! How can I assist you today?")
```
`result` is an `AIMessage` object. Access the generated string via `content` property:
```julia
typeof(result) # AIMessage{SubString{String}}
propertynames(result) # (:content, :status, :tokens, :elapsed
result.content # "Hello! How can I assist you today?"
```
___
You can use string interpolation:
```julia
a = 1
msg=aigenerate("What is `\$a+\$a`?")
msg.content # "The sum of `1+1` is `2`."
```
___
You can provide the whole conversation or more intricate prompts as a `Vector{AbstractMessage}`:
```julia
const PT = PromptingTools
conversation = [
PT.SystemMessage("You're master Yoda from Star Wars trying to help the user become a Yedi."),
PT.UserMessage("I have feelings for my iPhone. What should I do?")]
msg=aigenerate(conversation)
# AIMessage("Ah, strong feelings you have for your iPhone. A Jedi's path, this is not... <continues>")
```
"""
function aigenerate(prompt_schema::AbstractOpenAISchema, prompt::ALLOWED_PROMPT_TYPE;
verbose::Bool = true,
api_key::String = OPENAI_API_KEY,
model::String = MODEL_CHAT, return_all::Bool = false, dry_run::Bool = false,
conversation::AbstractVector{<:AbstractMessage} = AbstractMessage[],
http_kwargs::NamedTuple = (retry_non_idempotent = true,
retries = 5,
readtimeout = 120), api_kwargs::NamedTuple = NamedTuple(),
kwargs...)
##
global MODEL_ALIASES
## Find the unique ID for the model alias provided
model_id = get(MODEL_ALIASES, model, model)
conv_rendered = render(prompt_schema, prompt; conversation, kwargs...)
if !dry_run
time = @elapsed r = create_chat(prompt_schema, api_key,
model_id,
conv_rendered;
http_kwargs,
api_kwargs...)
## Process one of more samples returned
msg = if length(r.response[:choices]) > 1
run_id = Int(rand(Int32)) # remember one run ID
## extract all message
msgs = [response_to_message(prompt_schema, AIMessage, choice, r;
time, model_id, run_id, sample_id = i)
for (i, choice) in enumerate(r.response[:choices])]
## Order by log probability if available
## bigger is better, keep it last
if all(x -> !isnothing(x.log_prob), msgs)
sort(msgs, by = x -> x.log_prob)
else
msgs
end
else
## only 1 sample / 1 completion
choice = r.response[:choices][begin]
response_to_message(prompt_schema, AIMessage, choice, r;
time, model_id)
end
## Reporting
verbose && @info _report_stats(msg, model_id)
else
msg = nothing
end
## Select what to return
output = finalize_outputs(prompt,
conv_rendered,
msg;
conversation,
return_all,
dry_run,
kwargs...)
return output
end
"""
aiembed(prompt_schema::AbstractOpenAISchema,
doc_or_docs::Union{AbstractString, AbstractVector{<:AbstractString}},
postprocess::F = identity;
verbose::Bool = true,
api_key::String = OPENAI_API_KEY,
model::String = MODEL_EMBEDDING,
http_kwargs::NamedTuple = (retry_non_idempotent = true,
retries = 5,
readtimeout = 120),
api_kwargs::NamedTuple = NamedTuple(),
kwargs...) where {F <: Function}
The `aiembed` function generates embeddings for the given input using a specified model and returns a message object containing the embeddings, status, token count, and elapsed time.
## Arguments
- `prompt_schema::AbstractOpenAISchema`: The schema for the prompt.
- `doc_or_docs::Union{AbstractString, AbstractVector{<:AbstractString}}`: The document or list of documents to generate embeddings for.
- `postprocess::F`: The post-processing function to apply to each embedding. Defaults to the identity function.
- `verbose::Bool`: A flag indicating whether to print verbose information. Defaults to `true`.
- `api_key::String`: The API key to use for the OpenAI API. Defaults to `OPENAI_API_KEY`.
- `model::String`: The model to use for generating embeddings. Defaults to `MODEL_EMBEDDING`.
- `http_kwargs::NamedTuple`: Additional keyword arguments for the HTTP request. Defaults to `(retry_non_idempotent = true, retries = 5, readtimeout = 120)`.
- `api_kwargs::NamedTuple`: Additional keyword arguments for the OpenAI API. Defaults to an empty `NamedTuple`.
- `kwargs...`: Additional keyword arguments.
## Returns
- `msg`: A `DataMessage` object containing the embeddings, status, token count, and elapsed time. Use `msg.content` to access the embeddings.
# Example
```julia
msg = aiembed("Hello World")
msg.content # 1536-element JSON3.Array{Float64...
```
We can embed multiple strings at once and they will be `hcat` into a matrix
(ie, each column corresponds to one string)
```julia
msg = aiembed(["Hello World", "How are you?"])
msg.content # 1536×2 Matrix{Float64}:
```
If you plan to calculate the cosine distance between embeddings, you can normalize them first:
```julia
using LinearAlgebra
msg = aiembed(["embed me", "and me too"], LinearAlgebra.normalize)
# calculate cosine distance between the two normalized embeddings as a simple dot product
msg.content' * msg.content[:, 1] # [1.0, 0.787]
```
"""
function aiembed(prompt_schema::AbstractOpenAISchema,
doc_or_docs::Union{AbstractString, AbstractVector{<:AbstractString}},
postprocess::F = identity; verbose::Bool = true,
api_key::String = OPENAI_API_KEY,
model::String = MODEL_EMBEDDING,
http_kwargs::NamedTuple = (retry_non_idempotent = true,
retries = 5,
readtimeout = 120), api_kwargs::NamedTuple = NamedTuple(),
kwargs...) where {F <: Function}
##
global MODEL_ALIASES
## Find the unique ID for the model alias provided
model_id = get(MODEL_ALIASES, model, model)
time = @elapsed r = create_embeddings(prompt_schema, api_key,
doc_or_docs,
model_id;
http_kwargs,
api_kwargs...)
tokens_prompt = get(r.response, :usage, Dict(:prompt_tokens => 0))[:prompt_tokens]
msg = DataMessage(;
content = mapreduce(x -> postprocess(x[:embedding]), hcat, r.response[:data]),
status = Int(r.status),
cost = call_cost(tokens_prompt, 0, model_id),
tokens = (tokens_prompt, 0),
elapsed = time)
## Reporting
verbose && @info _report_stats(msg, model_id)
return msg
end
"Token IDs for GPT3.5 and GPT4 from https://platform.openai.com/tokenizer"
const OPENAI_TOKEN_IDS = Dict("true" => 837,
"false" => 905,
"unknown" => 9987,
"other" => 1023,
"1" => 16,
"2" => 17,
"3" => 18,
"4" => 19,
"5" => 20,
"6" => 21,
"7" => 22,
"8" => 23,
"9" => 24,
"10" => 605,
"11" => 806,
"12" => 717,
"13" => 1032,
"14" => 975,
"15" => 868,
"16" => 845,
"17" => 1114,
"18" => 972,
"19" => 777,
"20" => 508,
"21" => 1691,
"22" => 1313,
"23" => 1419,
"24" => 1187,
"25" => 914,
"26" => 1627,
"27" => 1544,
"28" => 1591,
"29" => 1682,
"30" => 966,
"31" => 2148,
"32" => 843,
"33" => 1644,
"34" => 1958,
"35" => 1758,
"36" => 1927,
"37" => 1806,
"38" => 1987,
"39" => 2137,
"40" => 1272
)
"""
encode_choices(schema::OpenAISchema, choices::AbstractVector{<:AbstractString}; kwargs...)
encode_choices(schema::OpenAISchema, choices::AbstractVector{T};
kwargs...) where {T <: Tuple{<:AbstractString, <:AbstractString}}
Encode the choices into an enumerated list that can be interpolated into the prompt and creates the corresponding logit biases (to choose only from the selected tokens).
Optionally, can be a vector tuples, where the first element is the choice and the second is the description.
There can be at most 40 choices provided.
# Arguments
- `schema::OpenAISchema`: The OpenAISchema object.
- `choices::AbstractVector{<:Union{AbstractString,Tuple{<:AbstractString, <:AbstractString}}}`: The choices to be encoded, represented as a vector of the choices directly, or tuples where each tuple contains a choice and its description.
- `kwargs...`: Additional keyword arguments.
# Returns
- `choices_prompt::AbstractString`: The encoded choices as a single string, separated by newlines.
- `logit_bias::Dict`: The logit bias dictionary, where the keys are the token IDs and the values are the bias values.
- `decode_ids::AbstractVector{<:AbstractString}`: The decoded IDs of the choices.
# Examples
```julia
choices_prompt, logit_bias, _ = PT.encode_choices(PT.OpenAISchema(), ["true", "false"])
choices_prompt # Output: "true for \"true\"\nfalse for \"false\"
logit_bias # Output: Dict(837 => 100, 905 => 100)
choices_prompt, logit_bias, _ = PT.encode_choices(PT.OpenAISchema(), ["animal", "plant"])
choices_prompt # Output: "1. \"animal\"\n2. \"plant\""
logit_bias # Output: Dict(16 => 100, 17 => 100)
```
Or choices with descriptions:
```julia
choices_prompt, logit_bias, _ = PT.encode_choices(PT.OpenAISchema(), [("A", "any animal or creature"), ("P", "for any plant or tree"), ("O", "for everything else")])
choices_prompt # Output: "1. \"A\" for any animal or creature\n2. \"P\" for any plant or tree\n3. \"O\" for everything else"
logit_bias # Output: Dict(16 => 100, 17 => 100, 18 => 100)
```
"""
function encode_choices(schema::OpenAISchema,
choices::AbstractVector{<:AbstractString};
kwargs...)
global OPENAI_TOKEN_IDS
## if all choices are in the dictionary, use the dictionary
if all(x -> haskey(OPENAI_TOKEN_IDS, x), choices)
choices_prompt = ["$c for \"$c\"" for c in choices]
logit_bias = Dict(OPENAI_TOKEN_IDS[c] => 100 for c in choices)
elseif length(choices) <= 40
## encode choices to IDs 1..40
choices_prompt = ["$(i). \"$c\"" for (i, c) in enumerate(choices)]
logit_bias = Dict(OPENAI_TOKEN_IDS[string(i)] => 100 for i in 1:length(choices))
else
throw(ArgumentError("The number of choices must be less than or equal to 20."))
end
return join(choices_prompt, "\n"), logit_bias, choices
end
function encode_choices(schema::OpenAISchema,
choices::AbstractVector{T};
kwargs...) where {T <: Tuple{<:AbstractString, <:AbstractString}}
global OPENAI_TOKEN_IDS
## if all choices are in the dictionary, use the dictionary
if all(x -> haskey(OPENAI_TOKEN_IDS, first(x)), choices)
choices_prompt = ["$c for \"$desc\"" for (c, desc) in choices]
logit_bias = Dict(OPENAI_TOKEN_IDS[c] => 100 for (c, desc) in choices)
elseif length(choices) <= 20
## encode choices to IDs 1..20
choices_prompt = ["$(i). \"$c\" for $desc" for (i, (c, desc)) in enumerate(choices)]
logit_bias = Dict(OPENAI_TOKEN_IDS[string(i)] => 100 for i in 1:length(choices))
else
throw(ArgumentError("The number of choices must be less than or equal to 20."))
end
return join(choices_prompt, "\n"), logit_bias, first.(choices)
end
# For testing
function encode_choices(schema::TestEchoOpenAISchema, choices; kwargs...)
return encode_choices(OpenAISchema(), choices; kwargs...)
end
# For testing
function decode_choices(schema::TestEchoOpenAISchema,
choices,
conv::Union{AbstractVector, AIMessage};
kwargs...)
return decode_choices(OpenAISchema(), choices, conv; kwargs...)
end
function decode_choices(schema::OpenAISchema, choices, conv::AbstractVector; kwargs...)
if length(conv) > 0 && last(conv) isa AIMessage && hasproperty(last(conv), :run_id)
## if it is a multi-sample response,
## Remember its run ID and convert all samples in that run
run_id = last(conv).run_id
for i in eachindex(conv)
msg = conv[i]
if isaimessage(msg) && msg.run_id == run_id
conv[i] = decode_choices(schema, choices, msg)
end
end
end
return conv
end
"""
decode_choices(schema::OpenAISchema,
choices::AbstractVector{<:AbstractString},
msg::AIMessage; kwargs...)
Decodes the underlying AIMessage against the original choices to lookup what the category name was.
If it fails, it will return `msg.content == nothing`
"""
function decode_choices(schema::OpenAISchema,
choices::AbstractVector{<:AbstractString},
msg::AIMessage; kwargs...)
global OPENAI_TOKEN_IDS
parsed_digit = tryparse(Int, strip(msg.content))
if !isnothing(parsed_digit) && haskey(OPENAI_TOKEN_IDS, strip(msg.content))
## It's encoded
content = choices[parsed_digit]
elseif haskey(OPENAI_TOKEN_IDS, strip(msg.content))
## if it's NOT a digit, but direct mapping (eg, true/false), no changes!
content = strip(msg.content)
else
## failed decoding
content = nothing
end
## create a new object with all the same fields except for content
return AIMessage(; [f => getfield(msg, f) for f in fieldnames(typeof(msg))]..., content)
end
"""
aiclassify(prompt_schema::AbstractOpenAISchema, prompt::ALLOWED_PROMPT_TYPE;
choices::AbstractVector{T} = ["true", "false", "unknown"],
api_kwargs::NamedTuple = NamedTuple(),
kwargs...) where {T <: Union{AbstractString, Tuple{<:AbstractString, <:AbstractString}}}
Classifies the given prompt/statement into an arbitrary list of `choices`, which must be only the choices (vector of strings) or choices and descriptions are provided (vector of tuples, ie, `("choice","description")`).
It's quick and easy option for "routing" and similar use cases, as it exploits the logit bias trick and outputs only 1 token.
classify into an arbitrary list of categories (including with descriptions). It's quick and easy option for "routing" and similar use cases, as it exploits the logit bias trick, so it outputs only 1 token.
!!! Note: The prompt/AITemplate must have a placeholder `choices` (ie, `{{choices}}`) that will be replaced with the encoded choices
Choices are rewritten into an enumerated list and mapped to a few known OpenAI tokens (maximum of 40 choices supported). Mapping of token IDs for GPT3.5/4 are saved in variable `OPENAI_TOKEN_IDS`.
It uses Logit bias trick and limits the output to 1 token to force the model to output only true/false/unknown. Credit for the idea goes to [AAAzzam](https://twitter.com/AAAzzam/status/1669753721574633473).
# Arguments
- `prompt_schema::AbstractOpenAISchema`: The schema for the prompt.
- `prompt`: The prompt/statement to classify if it's a `String`. If it's a `Symbol`, it is expanded as a template via `render(schema,template)`. Eg, templates `:JudgeIsItTrue` or `:InputClassifier`
- `choices::AbstractVector{T}`: The choices to be classified into. It can be a vector of strings or a vector of tuples, where the first element is the choice and the second is the description.
# Example
Given a user input, pick one of the two provided categories:
```julia
choices = ["animal", "plant"]
input = "Palm tree"
aiclassify(:InputClassifier; choices, input)
```
Choices with descriptions provided as tuples:
```julia
choices = [("A", "any animal or creature"), ("P", "any plant or tree"), ("O", "anything else")]
# try the below inputs:
input = "spider" # -> returns "A" for any animal or creature
input = "daphodil" # -> returns "P" for any plant or tree
input = "castle" # -> returns "O" for everything else
aiclassify(:InputClassifier; choices, input)
```
You could also use this function for routing questions to different endpoints (notice the different template and placeholder used), eg,
```julia
choices = [("A", "any question about animal or creature"), ("P", "any question about plant or tree"), ("O", "anything else")]
question = "how many spiders are there?"
msg = aiclassify(:QuestionRouter; choices, question)
# "A"
```
You can still use a simple true/false classification:
```julia
aiclassify("Is two plus two four?") # true
aiclassify("Is two plus three a vegetable on Mars?") # false
```
`aiclassify` returns only true/false/unknown. It's easy to get the proper `Bool` output type out with `tryparse`, eg,
```julia
tryparse(Bool, aiclassify("Is two plus two four?")) isa Bool # true
```
Output of type `Nothing` marks that the model couldn't classify the statement as true/false.
Ideally, we would like to re-use some helpful system prompt to get more accurate responses.
For this reason we have templates, eg, `:JudgeIsItTrue`. By specifying the template, we can provide our statement as the expected variable (`it` in this case)
See that the model now correctly classifies the statement as "unknown".
```julia
aiclassify(:JudgeIsItTrue; it = "Is two plus three a vegetable on Mars?") # unknown
```
For better results, use higher quality models like gpt4, eg,
```julia
aiclassify(:JudgeIsItTrue;
it = "If I had two apples and I got three more, I have five apples now.",
model = "gpt4") # true
```
"""
function aiclassify(prompt_schema::AbstractOpenAISchema, prompt::ALLOWED_PROMPT_TYPE;
choices::AbstractVector{T} = ["true", "false", "unknown"],
api_kwargs::NamedTuple = NamedTuple(),
kwargs...) where {T <:
Union{AbstractString, Tuple{<:AbstractString, <:AbstractString}}}
## Encode the choices and the corresponding prompt
## TODO: maybe check the model provided as well?
choices_prompt, logit_bias, decode_ids = encode_choices(prompt_schema, choices)
## We want only 1 token
api_kwargs = merge(api_kwargs, (; logit_bias, max_tokens = 1, temperature = 0))
msg_or_conv = aigenerate(prompt_schema,
prompt;
choices = choices_prompt,
api_kwargs,
kwargs...)
return decode_choices(prompt_schema, decode_ids, msg_or_conv)
end
function response_to_message(schema::AbstractOpenAISchema,
MSG::Type{DataMessage},
choice,
resp;
return_type = nothing,
model_id::AbstractString = "",
time::Float64 = 0.0,
run_id::Int = Int(rand(Int32)),
sample_id::Union{Nothing, Integer} = nothing)
@assert !isnothing(return_type) "You must provide a return_type for DataMessage construction"
## extract sum log probability
has_log_prob = haskey(choice, :logprobs) &&
!isnothing(get(choice, :logprobs, nothing)) &&
haskey(choice[:logprobs], :content) &&
!isnothing(choice[:logprobs][:content])
log_prob = if has_log_prob
sum([get(c, :logprob, 0.0) for c in choice[:logprobs][:content]])
else
nothing
end
## calculate cost
tokens_prompt = get(resp.response, :usage, Dict(:prompt_tokens => 0))[:prompt_tokens]
tokens_completion = get(resp.response, :usage, Dict(:completion_tokens => 0))[:completion_tokens]
cost = call_cost(tokens_prompt, tokens_completion, model_id)
# "Safe" parsing of the response - it still fails if JSON is invalid
content = try
choice[:message][:tool_calls][1][:function][:arguments] |>
x -> JSON3.read(x, return_type)
catch e
@warn "There was an error parsing the response: $e. Using the raw response instead."
choice[:message][:tool_calls][1][:function][:arguments] |>
JSON3.read |> copy
end
## build DataMessage object
msg = MSG(;
content = content,
status = Int(resp.status),
cost,
run_id,
sample_id,
log_prob,
finish_reason = get(choice, :finish_reason, nothing),
tokens = (tokens_prompt,
tokens_completion),
elapsed = time)
end
"""
aiextract(prompt_schema::AbstractOpenAISchema, prompt::ALLOWED_PROMPT_TYPE;
return_type::Type,
verbose::Bool = true,
api_key::String = OPENAI_API_KEY,
model::String = MODEL_CHAT,
return_all::Bool = false, dry_run::Bool = false,
conversation::AbstractVector{<:AbstractMessage} = AbstractMessage[],
http_kwargs::NamedTuple = (retry_non_idempotent = true,
retries = 5,
readtimeout = 120), api_kwargs::NamedTuple = (;
tool_choice = "exact"),
kwargs...)
Extract required information (defined by a struct **`return_type`**) from the provided prompt by leveraging OpenAI function calling mode.
This is a perfect solution for extracting structured information from text (eg, extract organization names in news articles, etc.)
It's effectively a light wrapper around `aigenerate` call, which requires additional keyword argument `return_type` to be provided
and will enforce the model outputs to adhere to it.
# Arguments
- `prompt_schema`: An optional object to specify which prompt template should be applied (Default to `PROMPT_SCHEMA = OpenAISchema`)
- `prompt`: Can be a string representing the prompt for the AI conversation, a `UserMessage`, a vector of `AbstractMessage` or an `AITemplate`
- `return_type`: A **struct** TYPE representing the the information we want to extract. Do not provide a struct instance, only the type.
If the struct has a docstring, it will be provided to the model as well. It's used to enforce structured model outputs or provide more information.
- `verbose`: A boolean indicating whether to print additional information.
- `api_key`: A string representing the API key for accessing the OpenAI API.
- `model`: A string representing the model to use for generating the response. Can be an alias corresponding to a model ID defined in `MODEL_ALIASES`.
- `return_all::Bool=false`: If `true`, returns the entire conversation history, otherwise returns only the last message (the `AIMessage`).
- `dry_run::Bool=false`: If `true`, skips sending the messages to the model (for debugging, often used with `return_all=true`).
- `conversation`: An optional vector of `AbstractMessage` objects representing the conversation history. If not provided, it is initialized as an empty vector.
- `http_kwargs`: A named tuple of HTTP keyword arguments.
- `api_kwargs`: A named tuple of API keyword arguments.
- `tool_choice`: A string representing the tool choice to use for the API call. Usually, one of "auto","any","exact".
Defaults to `"exact"`, which is a made-up value to enforce the OpenAI requirements if we want one exact function.
Providers like Mistral, Together, etc. use `"any"` instead.
- `kwargs`: Prompt variables to be used to fill the prompt/template
# Returns
If `return_all=false` (default):