-
Notifications
You must be signed in to change notification settings - Fork 8
/
user_preferences.jl
806 lines (735 loc) · 36.1 KB
/
user_preferences.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
# Defines the important Globals, model registry and user preferences
# See below (eg, MODEL_REGISTRY, ModelSpec)
"""
PREFERENCES
You can set preferences for PromptingTools by setting environment variables or by using the `set_preferences!`.
It will create a `LocalPreferences.toml` file in your current directory and will reload your prefences from there.
Check your preferences by calling `get_preferences(key::String)`.
# Available Preferences (for `set_preferences!`)
- `OPENAI_API_KEY`: The API key for the OpenAI API. See [OpenAI's documentation](https://platform.openai.com/docs/quickstart?context=python) for more information.
- `MISTRALAI_API_KEY`: The API key for the Mistral AI API. See [Mistral AI's documentation](https://docs.mistral.ai/) for more information.
- `COHERE_API_KEY`: The API key for the Cohere API. See [Cohere's documentation](https://docs.cohere.com/docs/the-cohere-platform) for more information.
- `DATABRICKS_API_KEY`: The API key for the Databricks Foundation Model API. See [Databricks' documentation](https://docs.databricks.com/en/machine-learning/foundation-models/api-reference.html) for more information.
- `DATABRICKS_HOST`: The host for the Databricks API. See [Databricks' documentation](https://docs.databricks.com/en/machine-learning/foundation-models/api-reference.html) for more information.
- `TAVILY_API_KEY`: The API key for the Tavily Search API. Register [here](https://tavily.com/). See more information [here](https://docs.tavily.com/docs/tavily-api/rest_api).
- `GOOGLE_API_KEY`: The API key for Google Gemini models. Get yours from [here](https://ai.google.dev/). If you see a documentation page ("Available languages and regions for Google AI Studio and Gemini API"), it means that it's not yet available in your region.
- `ANTHROPIC_API_KEY`: The API key for the Anthropic API. Get yours from [here](https://www.anthropic.com/).
- `VOYAGE_API_KEY`: The API key for the Voyage API. Free tier is upto 50M tokens! Get yours from [here](https://dash.voyageai.com/api-keys).
- `GROQ_API_KEY`: The API key for the Groq API. Free in beta! Get yours from [here](https://console.groq.com/keys).
- `DEEPSEEK_API_KEY`: The API key for the DeepSeek API. Get \$5 credit when you join. Get yours from [here](https://platform.deepseek.com/api_keys).
- `MODEL_CHAT`: The default model to use for aigenerate and most ai* calls. See `MODEL_REGISTRY` for a list of available models or define your own.
- `MODEL_EMBEDDING`: The default model to use for aiembed (embedding documents). See `MODEL_REGISTRY` for a list of available models or define your own.
- `PROMPT_SCHEMA`: The default prompt schema to use for aigenerate and most ai* calls (if not specified in `MODEL_REGISTRY`). Set as a string, eg, `"OpenAISchema"`.
See `PROMPT_SCHEMA` for more information.
- `MODEL_ALIASES`: A dictionary of model aliases (`alias => full_model_name`). Aliases are used to refer to models by their aliases instead of their full names to make it more convenient to use them.
See `MODEL_ALIASES` for more information.
- `MAX_HISTORY_LENGTH`: The maximum length of the conversation history. Defaults to 5. Set to `nothing` to disable history.
See `CONV_HISTORY` for more information.
- `LOCAL_SERVER`: The URL of the local server to use for `ai*` calls. Defaults to `http://localhost:10897/v1`. This server is called when you call `model="local"`
See `?LocalServerOpenAISchema` for more information and examples.
- `LOG_DIR`: The directory to save the logs to, eg, when using `SaverSchema <: AbstractTracerSchema`. Defaults to `joinpath(pwd(), "log")`. Refer to `?SaverSchema` for more information on how it works and examples.
At the moment it is not possible to persist changes to `MODEL_REGISTRY` across sessions.
Define your `register_model!()` calls in your `startup.jl` file to make them available across sessions or put them at the top of your script.
# Available ENV Variables
- `OPENAI_API_KEY`: The API key for the OpenAI API.
- `MISTRALAI_API_KEY`: The API key for the Mistral AI API.
- `COHERE_API_KEY`: The API key for the Cohere API.
- `LOCAL_SERVER`: The URL of the local server to use for `ai*` calls. Defaults to `http://localhost:10897/v1`. This server is called when you call `model="local"`
- `DATABRICKS_API_KEY`: The API key for the Databricks Foundation Model API.
- `DATABRICKS_HOST`: The host for the Databricks API.
- `TAVILY_API_KEY`: The API key for the Tavily Search API. Register [here](https://tavily.com/). See more information [here](https://docs.tavily.com/docs/tavily-api/rest_api).
- `GOOGLE_API_KEY`: The API key for Google Gemini models. Get yours from [here](https://ai.google.dev/). If you see a documentation page ("Available languages and regions for Google AI Studio and Gemini API"), it means that it's not yet available in your region.
- `ANTHROPIC_API_KEY`: The API key for the Anthropic API. Get yours from [here](https://www.anthropic.com/).
- `VOYAGE_API_KEY`: The API key for the Voyage API. Free tier is upto 50M tokens! Get yours from [here](https://dash.voyageai.com/api-keys).
- `GROQ_API_KEY`: The API key for the Groq API. Free in beta! Get yours from [here](https://console.groq.com/keys).
- `DEEPSEEK_API_KEY`: The API key for the DeepSeek API. Get \$5 credit when you join. Get yours from [here](https://platform.deepseek.com/api_keys).
- `LOG_DIR`: The directory to save the logs to, eg, when using `SaverSchema <: AbstractTracerSchema`. Defaults to `joinpath(pwd(), "log")`. Refer to `?SaverSchema` for more information on how it works and examples.
Preferences.jl takes priority over ENV variables, so if you set a preference, it will take precedence over the ENV variable.
WARNING: NEVER EVER sync your `LocalPreferences.toml` file! It contains your API key and other sensitive information!!!
"""
const PREFERENCES = nothing
"Keys that are allowed to be set via `set_preferences!`"
const ALLOWED_PREFERENCES = ["MISTRALAI_API_KEY",
"OPENAI_API_KEY",
"COHERE_API_KEY",
"DATABRICKS_API_KEY",
"DATABRICKS_HOST",
"TAVILY_API_KEY",
"GOOGLE_API_KEY",
"ANTHROPIC_API_KEY",
"VOYAGE_API_KEY",
"GROQ_API_KEY",
"DEEPSEEK_API_KEY",
"MODEL_CHAT",
"MODEL_EMBEDDING",
"MODEL_ALIASES",
"PROMPT_SCHEMA",
"MAX_HISTORY_LENGTH",
"LOCAL_SERVER",
"LOG_DIR"]
"""
set_preferences!(pairs::Pair{String, <:Any}...)
Set preferences for PromptingTools. See `?PREFERENCES` for more information.
See also: `get_preferences`
# Example
Change your API key and default model:
```julia
PromptingTools.set_preferences!("OPENAI_API_KEY" => "key1", "MODEL_CHAT" => "chat1")
```
"""
function set_preferences!(pairs::Pair{String, <:Any}...)
global ALLOWED_PREFERENCES
for (key, value) in pairs
@assert key in ALLOWED_PREFERENCES "Unknown preference '$key'! (Allowed preferences: $(join(ALLOWED_PREFERENCES,", "))"
@set_preferences!(key=>value)
if key == "MODEL_ALIASES" || key == "PROMPT_SCHEMA"
# cannot change in the same session
continue
else
setproperty!(@__MODULE__, Symbol(key), value)
end
end
@info("Preferences set; restart your Julia session for this change to take effect!")
end
"""
get_preferences(key::String)
Get preferences for PromptingTools. See `?PREFERENCES` for more information.
See also: `set_preferences!`
# Example
```julia
PromptingTools.get_preferences("MODEL_CHAT")
```
"""
function get_preferences(key::String)
global ALLOWED_PREFERENCES
@assert key in ALLOWED_PREFERENCES "Unknown preference '$key'! (Allowed preferences: $(join(ALLOWED_PREFERENCES,", "))"
getproperty(@__MODULE__, Symbol(key))
end
## Load up GLOBALS
const MODEL_CHAT::String = @load_preference("MODEL_CHAT", default="gpt-3.5-turbo")
const MODEL_EMBEDDING::String = @load_preference("MODEL_EMBEDDING",
default="text-embedding-3-small")
const MODEL_IMAGE_GENERATION = @load_preference("MODEL_IMAGE_GENERATION",
default="dall-e-3")
# the prompt schema default is defined in llm_interace.jl !
# const PROMPT_SCHEMA = OpenAISchema()
# First, load from preferences, then from environment variables
# Note: We load first into a variable `temp_` to avoid inlining of the get(ENV...) call
_temp = get(ENV, "OPENAI_API_KEY", "")
const OPENAI_API_KEY::String = @load_preference("OPENAI_API_KEY",
default=_temp);
# Note: Disable this warning by setting OPENAI_API_KEY to anything
isempty(OPENAI_API_KEY) &&
@warn "OPENAI_API_KEY variable not set! OpenAI models will not be available - set API key directly via `PromptingTools.OPENAI_API_KEY=<api-key>`!"
_temp = get(ENV, "MISTRALAI_API_KEY", "")
const MISTRALAI_API_KEY::String = @load_preference("MISTRALAI_API_KEY",
default=_temp);
_temp = get(ENV, "COHERE_API_KEY", "")
const COHERE_API_KEY::String = @load_preference("COHERE_API_KEY",
default=_temp);
_temp = get(ENV, "DATABRICKS_API_KEY", "")
const DATABRICKS_API_KEY::String = @load_preference("DATABRICKS_API_KEY",
default=_temp);
_temp = get(ENV, "DATABRICKS_HOST", "")
const DATABRICKS_HOST::String = @load_preference("DATABRICKS_HOST",
default=_temp);
_temp = get(ENV, "TAVILY_API_KEY", "")
const TAVILY_API_KEY::String = @load_preference("TAVILY_API_KEY",
default=_temp);
_temp = get(ENV, "GOOGLE_API_KEY", "")
const GOOGLE_API_KEY::String = @load_preference("GOOGLE_API_KEY",
default=_temp);
_temp = get(ENV, "TOGETHER_API_KEY", "")
const TOGETHER_API_KEY::String = @load_preference("TOGETHER_API_KEY",
default=_temp);
_temp = get(ENV, "FIREWORKS_API_KEY", "")
const FIREWORKS_API_KEY::String = @load_preference("FIREWORKS_API_KEY",
default=_temp);
_temp = get(ENV, "ANTHROPIC_API_KEY", "")
const ANTHROPIC_API_KEY::String = @load_preference("ANTHROPIC_API_KEY",
default=_temp);
_temp = get(ENV, "VOYAGE_API_KEY", "")
const VOYAGE_API_KEY::String = @load_preference("VOYAGE_API_KEY",
default=_temp);
_temp = get(ENV, "GROQ_API_KEY", "")
const GROQ_API_KEY::String = @load_preference("GROQ_API_KEY",
default=_temp);
_temp = get(ENV, "DEEPSEEK_API_KEY", "")
const DEEPSEEK_API_KEY::String = @load_preference("DEEPSEEK_API_KEY",
default=_temp);
_temp = get(ENV, "LOCAL_SERVER", "http://localhost:10897/v1")
## Address of the local server
const LOCAL_SERVER::String = @load_preference("LOCAL_SERVER",
default=_temp);
_temp = get(ENV, "LOG_DIR", joinpath(pwd(), "log"))
## Address of the local server
const LOG_DIR::String = @load_preference("LOG_DIR",
default=_temp);
## CONVERSATION HISTORY
"""
CONV_HISTORY
Tracks the most recent conversations through the `ai_str macros`.
Preference available: MAX_HISTORY_LENGTH, which sets how many last messages should be remembered.
See also: `push_conversation!`, `resize_conversation!`
"""
const CONV_HISTORY = Vector{Vector{<:Any}}()
const CONV_HISTORY_LOCK = ReentrantLock()
const MAX_HISTORY_LENGTH = @load_preference("MAX_HISTORY_LENGTH",
default=5)::Union{Int, Nothing}
## Model registry
# A dictionary of model names and their specs (ie, name, costs per token, etc.)
# Model specs are saved in ModelSpec struct (see below)
### ModelSpec Functionality
"""
ModelSpec
A struct that contains information about a model, such as its name, schema, cost per token, etc.
# Fields
- `name::String`: The name of the model. This is the name that will be used to refer to the model in the `ai*` functions.
- `schema::AbstractPromptSchema`: The schema of the model. This is the schema that will be used to generate prompts for the model, eg, `:OpenAISchema`.
- `cost_of_token_prompt::Float64`: The cost of 1 token in the prompt for this model. This is used to calculate the cost of a prompt.
Note: It is often provided online as cost per 1000 tokens, so make sure to convert it correctly!
- `cost_of_token_generation::Float64`: The cost of 1 token generated by this model. This is used to calculate the cost of a generation.
Note: It is often provided online as cost per 1000 tokens, so make sure to convert it correctly!
- `description::String`: A description of the model. This is used to provide more information about the model when it is queried.
# Example
```julia
spec = ModelSpec("gpt-3.5-turbo",
OpenAISchema(),
0.0015,
0.002,
"GPT-3.5 Turbo is a 175B parameter model and a common default on the OpenAI API.")
# register it
PromptingTools.register_model!(spec)
```
But you can also register any model directly via keyword arguments:
```julia
PromptingTools.register_model!(
name = "gpt-3.5-turbo",
schema = OpenAISchema(),
cost_of_token_prompt = 0.0015,
cost_of_token_generation = 0.002,
description = "GPT-3.5 Turbo is a 175B parameter model and a common default on the OpenAI API.")
```
"""
@kwdef mutable struct ModelSpec
name::String
schema::Union{AbstractPromptSchema, Nothing} = nothing
cost_of_token_prompt::Float64 = 0.0
cost_of_token_generation::Float64 = 0.0
description::String = ""
end
function Base.show(io::IO, m::ModelSpec)
dump(IOContext(io, :limit => true), m, maxdepth = 1)
end
"""
register_model!(registry = MODEL_REGISTRY;
name::String,
schema::Union{AbstractPromptSchema, Nothing} = nothing,
cost_of_token_prompt::Float64 = 0.0,
cost_of_token_generation::Float64 = 0.0,
description::String = "")
Register a new AI model with `name` and its associated `schema`.
Registering a model helps with calculating the costs and automatically selecting the right prompt schema.
# Arguments
- `name`: The name of the model. This is the name that will be used to refer to the model in the `ai*` functions.
- `schema`: The schema of the model. This is the schema that will be used to generate prompts for the model, eg, `OpenAISchema()`.
- `cost_of_token_prompt`: The cost of a token in the prompt for this model. This is used to calculate the cost of a prompt.
Note: It is often provided online as cost per 1000 tokens, so make sure to convert it correctly!
- `cost_of_token_generation`: The cost of a token generated by this model. This is used to calculate the cost of a generation.
Note: It is often provided online as cost per 1000 tokens, so make sure to convert it correctly!
- `description`: A description of the model. This is used to provide more information about the model when it is queried.
"""
function register_model!(registry = MODEL_REGISTRY;
name::String,
schema::Union{AbstractPromptSchema, Nothing} = nothing,
cost_of_token_prompt::Float64 = 0.0,
cost_of_token_generation::Float64 = 0.0,
description::String = "")
spec = ModelSpec(name,
schema,
cost_of_token_prompt,
cost_of_token_generation,
description)
register_model!(spec; registry)
end
function register_model!(spec::ModelSpec; registry = MODEL_REGISTRY)
haskey(registry, spec.name) &&
@warn "Model `$(spec.name)` already registered! It will be overwritten."
registry[spec.name] = spec
end
## Model Registry Data
### Model Aliases
# global reference MODEL_ALIASES is defined below
aliases = merge(
Dict("gpt3" => "gpt-3.5-turbo",
"gpt4" => "gpt-4",
"gpt4o" => "gpt-4o",
"gpt4v" => "gpt-4-vision-preview", # 4v is for "4 vision"
"gpt4t" => "gpt-4-turbo", # 4t is for "4 turbo"
"gpt3t" => "gpt-3.5-turbo-0125", # 3t is for "3 turbo"
"ada" => "text-embedding-ada-002",
"emb3small" => "text-embedding-3-small",
"emb3large" => "text-embedding-3-large",
"yi34c" => "yi:34b-chat",
"oh25" => "openhermes2.5-mistral",
"starling" => "starling-lm",
"llama3" => "llama3:8b-instruct-q5_K_S",
# o-llama3, because it's hosted on Ollama (same as t-mixtral on Together)
"ollama3" => "llama3:8b-instruct-q5_K_S",
"local" => "local-server",
"gemini" => "gemini-pro",
## f-mixtral -> Fireworks.ai Mixtral
"fmixtral" => "accounts/fireworks/models/mixtral-8x7b-instruct",
"firefunction" => "accounts/fireworks/models/firefunction-v1",
## t-mixtral -> Together.ai Mixtral
"tmixtral" => "mistralai/Mixtral-8x7B-Instruct-v0.1",
"tmixtral22" => "mistralai/Mixtral-8x22B-Instruct-v0.1",
"tllama3" => "meta-llama/Llama-3-8b-chat-hf",
"tllama370" => "meta-llama/Llama-3-70b-chat-hf",
## Mistral AI
"mistral-tiny" => "mistral-tiny",
"mistral-small" => "mistral-small-latest",
"mistral-medium" => "mistral-medium-latest",
"mistral-large" => "mistral-large-latest",
"mistralt" => "mistral-tiny",
"mistrals" => "mistral-small-latest",
"mistralm" => "mistral-medium-latest",
"mistrall" => "mistral-large-latest",
## Default to Sonnet as a the medium offering
"claude" => "claude-3-sonnet-20240229",
"claudeo" => "claude-3-opus-20240229",
"claudes" => "claude-3-sonnet-20240229",
"claudeh" => "claude-3-haiku-20240307",
## Groq
"gllama3" => "llama3-8b-8192",
"gl3" => "llama3-8b-8192",
"gllama370" => "llama3-70b-8192",
"gl70" => "llama3-70b-8192",
"gmixtral" => "mixtral-8x7b-32768",
## DeepSeek
"dschat" => "deepseek-chat",
"dscode" => "deepseek-coder"
),
## Load aliases from preferences as well
@load_preference("MODEL_ALIASES", default=Dict{String, String}()))
registry = Dict{String, ModelSpec}(
"gpt-3.5-turbo" => ModelSpec("gpt-3.5-turbo",
OpenAISchema(),
0.5e-6,
1.5e-6,
"GPT-3.5 Turbo is a 175B parameter model and a common default on the OpenAI API. From mid-Feb 2024, it will be using the new GPT-3.5 Turbo 0125 version (pricing is set assuming the 0125 version)."),
"gpt-3.5-turbo-1106" => ModelSpec("gpt-3.5-turbo-1106",
OpenAISchema(),
1e-6,
2e-6,
"GPT-3.5 Turbo is an updated version of GPT3.5 that is much faster and cheaper to use. 1106 refers to the release date of November 6, 2023."),
"gpt-3.5-turbo-0125" => ModelSpec("gpt-3.5-turbo-0125",
OpenAISchema(),
0.5e-6,
1.5e-6,
"GPT-3.5 Turbo is an updated version of GPT3.5 that is much faster and cheaper to use. This is the cheapest GPT-3.5 Turbo model. 0125 refers to the release date of January 25, 2024."),
"gpt-4" => ModelSpec("gpt-4",
OpenAISchema(),
3e-5,
6e-5,
"GPT-4 is a 1.75T parameter model and the largest model available on the OpenAI API."),
"gpt-4-1106-preview" => ModelSpec("gpt-4-1106-preview",
OpenAISchema(),
1e-5,
3e-5,
"GPT-4 Turbo 1106 is an updated version of GPT4 that is much faster and the cheaper to use. 1106 refers to the release date of November 6, 2023."),
"gpt-4-0125-preview" => ModelSpec("gpt-4-0125-preview",
OpenAISchema(),
1e-5,
3e-5,
"GPT-4 Turbo is an updated version of GPT4 that is much faster and the cheaper to use. 0125 refers to the release date of January 25, 2024."),
"gpt-4-turbo" => ModelSpec("gpt-4-turbo",
OpenAISchema(),
1e-5,
3e-5,
"GPT-4 Turbo is an updated version of GPT4 that is much faster and the cheaper to use. This is the general name for whatever is the latest GPT4 Turbo preview release. In April-24, it points to version 2024-04-09."),
"gpt-4-turbo-2024-04-09" => ModelSpec("gpt-4-turbo-2024-04-09",
OpenAISchema(),
1e-5,
3e-5,
"GPT-4 Turbo is an updated version of GPT4 that is much faster and the cheaper to use. 2024-04-09 refers to the release date of 9th April 2024 with knowledge upto December 2023."),
"gpt-4-turbo-preview" => ModelSpec("gpt-4-turbo-preview",
OpenAISchema(),
1e-5,
3e-5,
"GPT-4 Turbo is an updated version of GPT4 that is much faster and the cheaper to use. This is the general name for whatever is the latest GPT4 Turbo preview release. Right now it is 0125."),
"gpt-4o-2024-05-13" => ModelSpec("gpt-4o-2024-05-13",
OpenAISchema(),
5e-6,
1.5e-5,
"GPT-4 Omni, the latest GPT4 model that is faster and cheaper than GPT-4 Turbo is an updated version of GPT4 that is much faster and the cheaper to use."),
"gpt-4o" => ModelSpec("gpt-4o",
OpenAISchema(),
5e-6,
1.5e-5,
"GPT-4 Omni, the latest GPT4 model that is faster and cheaper than GPT-4 Turbo is an updated version of GPT4 that is much faster and the cheaper to use. Currently points to version gpt-4o-2024-05-13."),
"gpt-4-vision-preview" => ModelSpec(
"gpt-4-vision-preview",
OpenAISchema(),
1e-5,
3e-5,
"GPT-4 Vision is similar to GPT-4 but it adds visual capabilities."),
"dall-e-3" => ModelSpec("dall-e-3",
OpenAISchema(),
0, ## tracked differently via ALTERNATIVE_GENERATION_COSTS
0, ## tracked differently via ALTERNATIVE_GENERATION_COSTS
"The best image generation model from OpenAI DALL-E 3. Note: Costs are tracked on per-image basis!"),
"dall-e-2" => ModelSpec("dall-e-2",
OpenAISchema(),
0, ## tracked differently via ALTERNATIVE_GENERATION_COSTS
0, ## tracked differently via ALTERNATIVE_GENERATION_COSTS
"Image generation model from OpenAI DALL-E 2. Note: Costs are tracked on per-image basis!"),
"text-embedding-ada-002" => ModelSpec("text-embedding-ada-002",
OpenAISchema(),
1e-7,
0.0,
"Classic text embedding endpoint Ada from 2022 with 1536 dimensions."),
"text-embedding-3-small" => ModelSpec("text-embedding-3-small",
OpenAISchema(),
0.2e-7,
0.0,
"New text embedding endpoint with 1536 dimensions, but 5x cheaper than Ada and more performant."),
"text-embedding-3-large" => ModelSpec("text-embedding-3-large",
OpenAISchema(),
1.3e-7,
0.0,
"New text embedding endpoint with 3072 dimensions, c. 30% more expensive than Ada but more performant."),
"llama2" => ModelSpec("llama2",
OllamaSchema(),
0.0,
0.0,
"LLAMA2 is a 7B parameter model from Meta."),
"openhermes2.5-mistral" => ModelSpec("openhermes2.5-mistral",
OllamaSchema(),
0.0,
0.0,
"OpenHermes 2.5 Mistral is a 7B parameter model finetuned by X on top of base model from Mistral AI."),
"starling-lm" => ModelSpec("starling-lm",
OllamaSchema(),
0.0,
0.0,
"Starling LM is a 7B parameter model finetuned by X on top of base model from Starling AI."),
"yi:34b-chat" => ModelSpec("yi:34b-chat",
OllamaSchema(),
0.0,
0.0,
"Yi is a 34B parameter model finetuned by X on top of base model from Starling AI."),
"llama3:8b-instruct-q5_K_S" => ModelSpec("llama3:8b-instruct-q5_K_S",
OllamaSchema(),
0.0,
0.0,
"Llama 3 8b is the latest model from Meta"
),
"wizardlm2:7b-q5_K_S" => ModelSpec("wizardlm2:7b-q5_K_S",
OllamaSchema(),
0.0,
0.0,
"WizardLM2 7b from Microsoft."),
"nomic-embed-text" => ModelSpec("nomic-embed-text",
OllamaSchema(),
0.0,
0.0,
"Ollama-hosted embedding model from Nomic with 127M parameters and 8K tokens context. Alleged to be competitive with OpenAI small embedding model."),
"mxbai-embed-large" => ModelSpec("mxbai-embed-large",
OllamaSchema(),
0.0,
0.0,
"Ollama-hosted embedding model from MixedBread.ai with 334M parameters and 512 tokens context. Alleged to be competitive with OpenAI large embedding model."),
"llava" => ModelSpec("llava",
OllamaSchema(),
0.0,
0.0,
"A novel end-to-end trained large multimodal model that combines a vision encoder and Vicuna for general-purpose visual and language understanding."),
"bakllava" => ModelSpec("bakllava",
OllamaSchema(),
0.0, 0.0,
"BakLLaVA is a multimodal model consisting of the Mistral 7B base model augmented with the LLaVA architecture."),
"open-mistral-7b" => ModelSpec("open-mistral-7b",
MistralOpenAISchema(),
2.5e-7,
2.5e-7,
"Mistral AI's hosted version of openly available Mistral-7B-v0.2. Great for simple tasks."),
"open-mixtral-8x7b" => ModelSpec("open-mixtral-8x7b",
MistralOpenAISchema(),
7e-7,
7e-7,
"Mistral AI's hosted version of openly available Mixtral-8x7B-v0.1. Good for more complicated tasks."),
"mistral-tiny" => ModelSpec("mistral-tiny",
MistralOpenAISchema(),
2e-6,
6e-6,
"Mistral AI's own finetune of their 7b model."),
"mistral-tiny-2312" => ModelSpec("mistral-tiny-2312",
MistralOpenAISchema(),
2e-6,
6e-6,
"Mistral AI's own finetune of their 7b model. Version 2312."),
"mistral-small-latest" => ModelSpec("mistral-small-latest",
MistralOpenAISchema(),
2e-6,
6e-6,
"Mistral AI's own finetune (historically similar to Mixtral-8x7B)."),
"mistral-small-2402" => ModelSpec("mistral-small-2402",
MistralOpenAISchema(),
2e-6,
6e-6,
"Mistral AI's own finetune (historically similar to Mixtral-8x7B). Version 2402."),
"mistral-medium-latest" => ModelSpec("mistral-medium-latest",
MistralOpenAISchema(),
2.7e-6,
8.1e-6,
"Mistral AI's own model. Details unknown."),
"mistral-medium-2312" => ModelSpec("mistral-medium-2312",
MistralOpenAISchema(),
2.7e-6,
8.1e-6,
"Mistral AI's own model. Version 2312. Details unknown."),
"mistral-large-latest" => ModelSpec("mistral-large-latest",
MistralOpenAISchema(),
8e-6,
2.4e-5,
"Mistral AI's hosted version of their best model available. Details unknown."),
"mistral-large-2402" => ModelSpec("mistral-large-2402",
MistralOpenAISchema(),
8e-6,
2.4e-5,
"Mistral AI's hosted version of their best model available. Version 2402. Details unknown."),
"mistral-embed" => ModelSpec("mistral-embed",
MistralOpenAISchema(),
1e-7,
0.0,
"Mistral AI's hosted model for embeddings."),
"echo" => ModelSpec("echo",
TestEchoOpenAISchema(;
response = Dict(
:choices => [
Dict(:message => Dict(:content => "Hello!"),
:finish_reason => "stop")
],
:usage => Dict(:total_tokens => 3,
:prompt_tokens => 2,
:completion_tokens => 1)), status = 200),
0.0,
0.0,
"Echo is only for testing. It always responds with 'Hello!'"),
"local-server" => ModelSpec("local-server",
LocalServerOpenAISchema(),
0.0,
0.0,
"Local server, eg, powered by [Llama.jl](https://github.com/marcom/Llama.jl). Model is specified when instantiating the server itself. It will be automatically pointed to the address in `LOCAL_SERVER`."),
"custom" => ModelSpec("custom",
LocalServerOpenAISchema(),
0.0,
0.0,
"Send a generic request to a custom server. Make sure to explicitly define the `api_kwargs = (; url = ...)` when calling the model."),
"gemini-pro" => ModelSpec("gemini-pro",
GoogleSchema(),
0.0, #unknown, expected 1.25e-7
0.0, #unknown, expected 3.75e-7
"Gemini Pro is a LLM from Google. For more information, see [models](https://ai.google.dev/models/gemini)."),
"accounts/fireworks/models/mixtral-8x7b-instruct" => ModelSpec(
"accounts/fireworks/models/mixtral-8x7b-instruct",
FireworksOpenAISchema(),
5e-7,
5e-7,
"Mixtral (8x7b) from Mistral, hosted by Fireworks.ai. For more information, see [models](https://fireworks.ai/models/fireworks/mixtral-8x7b-instruct)."),
"accounts/fireworks/models/mixtral-8x22b-instruct-preview" => ModelSpec(
"accounts/fireworks/models/mixtral-8x22b-instruct-preview",
FireworksOpenAISchema(),
9e-7,
9e-7,
"Mixtral (8x22b) from Mistral, instruction finetuned and hosted by Fireworks.ai. For more information, see [models](https://fireworks.ai/models/fireworks/mixtral-8x22b-instruct-preview)."),
"accounts/fireworks/models/dbrx-instruct" => ModelSpec(
"accounts/fireworks/models/dbrx-instruct",
FireworksOpenAISchema(),
1.6e-6,
1.6e-6,
"Databricks DBRX Instruct, hosted by Fireworks.ai. For more information, see [models](https://fireworks.ai/models/fireworks/dbrx-instruct)."),
"accounts/fireworks/models/qwen-72b-chat" => ModelSpec(
"accounts/fireworks/models/qwen-72b-chat",
FireworksOpenAISchema(),
9e-7,
9e-7,
"Qwen is a 72B parameter model from Alibaba Cloud, hosted by from Fireworks.ai. For more information, see [models](https://fireworks.ai/models/fireworks/dbrx-instruct)."),
"accounts/fireworks/models/firefunction-v1" => ModelSpec(
"accounts/fireworks/models/firefunction-v1",
FireworksOpenAISchema(),
0.0, #unknown, expected to be the same as Mixtral
0.0, #unknown, expected to be the same as Mixtral
"Fireworks' open-source function calling model (fine-tuned Mixtral). Useful for `aiextract` calls. For more information, see [models](https://fireworks.ai/models/fireworks/firefunction-v1)."),
## Together AI
"mistralai/Mixtral-8x7B-Instruct-v0.1" => ModelSpec(
"mistralai/Mixtral-8x7B-Instruct-v0.1",
TogetherOpenAISchema(),
6e-7,
6e-7,
"Mixtral (8x7b) from Mistral, hosted by Together.ai. For more information, see [models](https://docs.together.ai/docs/inference-models)."),
"mistralai/Mixtral-8x22B-Instruct-v0.1" => ModelSpec(
"mistralai/Mixtral-8x22B-Instruct-v0.1",
TogetherOpenAISchema(),
1.2e-6,
1.2e-6,
"Mixtral (22x7b) from Mistral, hosted by Together.ai. For more information, see [models](https://docs.together.ai/docs/inference-models)."),
"meta-llama/Llama-3-8b-chat-hf" => ModelSpec(
"meta-llama/Llama-3-8b-chat-hf",
TogetherOpenAISchema(),
2e-7,
2e-7,
"Meta Llama3 8b from Mistral, hosted by Together.ai. For more information, see [models](https://docs.together.ai/docs/inference-models)."),
"meta-llama/Llama-3-70b-chat-hf" => ModelSpec(
"meta-llama/Llama-3-70b-chat-hf",
TogetherOpenAISchema(),
9e-7,
9e-7,
"Meta Llama3 70b from Mistral, hosted by Together.ai. For more information, see [models](https://docs.together.ai/docs/inference-models)."),
### Anthropic models
"claude-3-opus-20240229" => ModelSpec("claude-3-opus-20240229",
AnthropicSchema(),
1.5e-5,
7.5e-5,
"Anthropic's latest and strongest model Claude 3 Opus. Max output 4096 tokens, 200K context. See details [here](https://docs.anthropic.com/claude/docs/models-overview)"),
"claude-3-sonnet-20240229" => ModelSpec("claude-3-sonnet-20240229",
AnthropicSchema(),
3e-6,
1.5e-5,
"Anthropic's middle model Claude 3 Sonnet. Max output 4096 tokens, 200K context. See details [here](https://docs.anthropic.com/claude/docs/models-overview)"),
"claude-3-haiku-20240307" => ModelSpec("claude-3-haiku-20240307",
AnthropicSchema(),
2.5e-7,
1.25e-6,
"Anthropic's smallest and faster model Claude 3 Haiku. Max output 4096 tokens, 200K context. See details [here](https://docs.anthropic.com/claude/docs/models-overview)"),
"claude-2.1" => ModelSpec("claude-2.1",
AnthropicSchema(),
8e-6,
2.4e-5,
"Anthropic's Claude 2.1 model."),
## Groq -- using preliminary pricing on https://wow.groq.com/
"llama3-8b-8192" => ModelSpec("llama3-8b-8192",
GroqOpenAISchema(),
5e-8,
1e-7,
"Meta's Llama3 8b, hosted by Groq. Max output 8192 tokens, 8K context. See details [here](https://console.groq.com/docs/models)"),
"llama3-70b-8192" => ModelSpec("llama3-70b-8192",
GroqOpenAISchema(),
5.9e-7,
7.9e-7,
"Meta's Llama3 70b, hosted by Groq. Max output 8192 tokens, 8K context. See details [here](https://console.groq.com/docs/models)"),
"mixtral-8x7b-32768" => ModelSpec("mixtral-8x7b-32768",
GroqOpenAISchema(),
2.7e-7,
2.7e-7,
"Mistral.ai Mixtral 8x7b, hosted by Groq. Max 32K context. See details [here](https://console.groq.com/docs/models)"),
"deepseek-chat" => ModelSpec("deepseek-chat",
DeepSeekOpenAISchema(),
1.4e-7,
2.8e-7,
"Deepseek.com-hosted DeepSeekV2 model. Max 32K context. See details [here](https://platform.deepseek.com/docs)"),
"deepseek-coder" => ModelSpec("deepseek-coder",
DeepSeekOpenAISchema(),
1.4e-7,
2.8e-7,
"Deepseek.com-hosted coding model. Max 16K context. See details [here](https://platform.deepseek.com/docs)")
)
"""
ALTERNATIVE_GENERATION_COSTS
Tracker of alternative costing models, eg, for image generation (`dall-e-3`), the cost is driven by quality/size.
"""
ALTERNATIVE_GENERATION_COSTS = Dict{String, Any}(
"dall-e-3" => Dict(
"standard" => Dict(
"1024x1024" => 0.04, "1024x1792" => 0.08, "1792x1024" => 0.08),
"hd" => Dict(
"1024x1024" => 0.08, "1024x1792" => 0.12, "1792x1024" => 0.12)),
"dall-e-2" => Dict(
"standard" => Dict(
"1024x1024" => 0.02, "512x512" => 0.018, "256x256" => 0.016),
"hd" => Dict("1024x1024" => 0.02, "512x512" => 0.018, "256x256" => 0.016))
)
### Model Registry Structure
@kwdef mutable struct ModelRegistry
registry::Dict{String, ModelSpec}
aliases::Dict{String, String}
end
function Base.show(io::IO, registry::ModelRegistry)
num_models = length(registry.registry)
num_aliases = length(registry.aliases)
print(io,
"ModelRegistry with $num_models models and $num_aliases aliases. See `?MODEL_REGISTRY` for more information.")
end
"""
MODEL_REGISTRY
A store of available model names and their specs (ie, name, costs per token, etc.)
# Accessing the registry
You can use both the alias name or the full name to access the model spec:
```
PromptingTools.MODEL_REGISTRY["gpt-3.5-turbo"]
```
# Registering a new model
```julia
register_model!(
name = "gpt-3.5-turbo",
schema = :OpenAISchema,
cost_of_token_prompt = 0.0015,
cost_of_token_generation = 0.002,
description = "GPT-3.5 Turbo is a 175B parameter model and a common default on the OpenAI API.")
```
# Registering a model alias
```julia
PromptingTools.MODEL_ALIASES["gpt3"] = "gpt-3.5-turbo"
```
"""
const MODEL_REGISTRY = ModelRegistry(registry, aliases)
# We overload the getindex function to allow for lookup via model aliases
function Base.getindex(registry::ModelRegistry, key::String)
# Check if the key exists in the registry
if haskey(registry.registry, key)
return registry.registry[key]
end
# If the key is not in the registry, check if it's an alias
aliased_key = get(registry.aliases, key, nothing)
if !isnothing(aliased_key) && haskey(registry.registry, aliased_key)
return registry.registry[aliased_key]
end
# Handle the case where the key is neither in the registry nor an alias
throw(KeyError("Model with key '$key' not found in PromptingTools.MODEL_REGISTRY."))
end
function Base.setindex!(registry::ModelRegistry, value::ModelSpec, key::String)
registry.registry[key] = value
end
function Base.haskey(registry::ModelRegistry, key::String)
haskey(registry.registry, key) || haskey(registry.aliases, key)
end
function Base.get(registry::ModelRegistry, key::String, default)
if haskey(registry, key)
return registry[key]
else
return default
end
end
function Base.delete!(registry::ModelRegistry, key::String)
haskey(registry.registry, key) && delete!(registry.registry, key)
haskey(registry.aliases, key) && delete!(registry.aliases, key)
return registry
end
"Shows the list of models in the registry. Add more with `register_model!`."
list_registry() = sort(collect(keys(MODEL_REGISTRY.registry)))
"Shows the Dictionary of model aliases in the registry. Add more with `MODEL_ALIASES[alias] = model_name`."
list_aliases() = MODEL_REGISTRY.aliases
"""
MODEL_ALIASES
A dictionary of model aliases. Aliases are used to refer to models by their aliases instead of their full names to make it more convenient to use them.
# Accessing the aliases
```
PromptingTools.MODEL_ALIASES["gpt3"]
```
# Register a new model alias
```julia
PromptingTools.MODEL_ALIASES["gpt3"] = "gpt-3.5-turbo"
```
"""
const MODEL_ALIASES = MODEL_REGISTRY.aliases