-
Notifications
You must be signed in to change notification settings - Fork 0
/
aiflow.py
1189 lines (980 loc) · 35.8 KB
/
aiflow.py
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
# aiflow.py
import json
from openai import OpenAI, OpenAIError
from typing import Optional, Callable, List, Dict, Union
from enum import Enum
from IPython.display import HTML
from docx import Document
import urllib.request
from pathlib import Path
import os
from IPython.display import Markdown, display
import markdown
from pydantic import BaseModel
# chroma helper that converts a query result to a string, so we can use it in the class
def chroma_query_result_to_text(obj):
documents = obj.get("documents")
if documents:
concatenated_string = "".join(["\n".join(doc) for doc in documents])
return concatenated_string
else:
return ""
# chroma helper that converts the query to a list
def chroma_query_to_list(result):
ids = result["ids"][0]
metadatas = result["metadatas"][0]
distances = result["distances"][0]
# combine each n-th item from ids, metadatas and distances and put it in an object. Append to a result list and return
if len(ids) != len(metadatas) or len(ids) != len(distances):
raise ValueError("Lengths of ids, metadatas, and distances must be the same")
result_list = []
for i in range(len(ids)):
# Create an object with id, metadata, and distance
item = {"id": ids[i], "metadata": metadatas[i], "distance": distances[i]}
# Append the object to the result list
result_list.append(item)
return result_list
class Model(Enum):
GPT_4 = "gpt-4"
GPT_4O = "gpt-4o"
GPT_4O_MINI = "gpt-4o-mini"
class AIFlow:
def __init__(
self,
api_key,
model=Model.GPT_4O_MINI,
temperature=0,
max_tokens=150,
):
self.client = OpenAI(api_key=api_key)
self.model = model.value
self.max_tokens = max_tokens
self.temperature = temperature
self.json_mode = False
self.completion_tokens = 0
self.prompt_tokens = 0
self.total_tokens = 0
self.chat_messages = []
self.context_map = {}
self.images_map = {}
self.audio_map = {}
self.default_folder_for_output = ""
self.verbose = True
self.latest_state_filename = ""
self.save_state_per_step = False
# model configs
def set_temperature(self, temperature: int = 0) -> "AIFlow":
"""
Set the temperature for the model.
:param temperature: Temperature value
:return: self
"""
self.temperature = temperature
return self
self.temperature = temperature
return self
def set_model(self, model: Union[Model, str] = Model.GPT_4) -> "AIFlow":
"""
Set the model to be used.
:param model: Model name
:return: self
"""
self.model = model.value
return self
def set_max_tokens(self, max_tokens: int = 150) -> "AIFlow":
"""
Set the maximum number of tokens.
:param max_tokens: Maximum tokens value
:return: self
"""
self.max_tokens = max_tokens
return self
def set_json_output(self, json_mode: bool = False) -> "AIFlow":
"""
Set the output format to JSON.
:param json_mode: Boolean flag for JSON mode
:return: self
"""
self.json_mode = json_mode
return self
def display_model_config(self) -> "AIFlow":
"""
Display the current model configuration.
:return: self
"""
print(f"Model: {self.model}")
print(f"Max Tokens: {self.max_tokens}")
print(f"Temperature: {self.temperature}")
return self
def get_token_usage(self) -> Dict[str, int]:
"""
Get the token usage statistics.
:return: Dictionary with token usage statistics
"""
return {
"completion_tokens": self.completion_tokens,
"prompt_tokens": self.prompt_tokens,
"total_tokens": self.total_tokens,
}
# other config
def set_output_folder(self, folder: str = "") -> "AIFlow":
"""
Set the default folder for output.
:param folder: Folder path
:return: self
"""
self.default_folder_for_output = folder
if folder != "":
os.makedirs(self.default_folder_for_output, exist_ok=True)
return self
def set_verbose(self, level: bool = True) -> "AIFlow":
"""
Set the verbosity level.
:param level: Boolean flag for verbosity
:return: self
"""
self.verbose = level
return self
def set_step_save(self, step: bool = False) -> "AIFlow":
"""
Enable or disable saving state per step.
:param step: Boolean flag for step save
:return: self
"""
self.save_state_per_step = step
return self
#
# Saving state
#
def save_internal_state(self, filename: str = "") -> "AIFlow":
"""
Save the internal state to a file.
:param filename: Name of the file to save the state
:return: self
"""
if filename == "" and self.latest_state_filename == "":
print("Error - no state filename provided")
return self
if filename == "":
filename = self.latest_state_filename
self.latest_state_filename = filename
# Create a copy of the current state
state_to_save = self.__dict__.copy()
# Remove objects that cannot be serialized
state_to_save.pop("client", None)
# Save the state to a JSON file
with open(filename, "w") as f:
json.dump(state_to_save, f, indent=4)
return self
def load_internal_state(self, filename: str = "state.json") -> "AIFlow":
"""
Load the internal state from a file.
:param filename: Name of the file to load the state from
:return: self
"""
self.latest_state_filename = filename
try:
with open(filename, "r") as f:
state = json.load(f)
self.__dict__.update(state)
except FileNotFoundError:
print(f"File '{filename}' not found.")
return self
#
# Some debugging tools
#
def display_internal_data(self) -> "AIFlow":
"""
Display internal data for debugging.
:return: self
"""
print("Chat Messages:")
print(json.dumps(self.chat_messages, indent=4))
print("\nContext Map:")
print(json.dumps(self.context_map, indent=4))
print("\nImages Map:")
print(json.dumps(self.images_map, indent=4))
print("\nAudio Map:")
print(json.dumps(self.audio_map, indent=4))
return self
def clear_internal_data(self) -> "AIFlow":
"""
Clear internal data.
:return: self
"""
self.chat_messages = []
self.context_map = {}
self.images_map = {}
self.audio_map = {}
return self
# function to run another function that may return something or nothing - this to support running code in the chain
def execute_function(
self, func: Callable[[], str] = lambda: "", label: str = ""
) -> "AIFlow":
"""
Run a function that may return something or nothing.
:param func: Function to run
:param label: Label for the context
:return: self
"""
return self
#
# Chat methods
#
def pretty_print_messages(self) -> "AIFlow":
"""
Pretty print chat messages.
:return: self
"""
for message in self.chat_messages:
role = message["role"]
content = message["content"]
print(f"{role}:")
print(content)
print()
return self
def pretty_print_messages_to_file(
self, file_name: str = "output.txt", html: bool = True
) -> "AIFlow":
"""
Pretty print chat messages to a file.
:param file_name: Name of the file to save the messages
:param html: Whether to return HTML for downloading the file
:return: self
"""
with open(file_name, "w") as file:
for message in self.chat_messages:
role = message["role"]
content = message["content"]
file.write(f"{role}:\n")
file.write(content + "\n\n")
if html:
return HTML(
f'<a href="{file_name}" download>Click here to download the pretty-printed messages</a>'
)
return self
def set_system_prompt(self, prompt: str = "") -> "AIFlow":
"""
Set the system prompt.
:param prompt: System prompt
:return: self
"""
# Remove existing "system" role message if it exists
self.chat_messages = [
msg for msg in self.chat_messages if msg.get("role") != "system"
]
if self.verbose:
print(prompt)
if self.save_state_per_step:
self.save_internal_state()
prompt = self.replace_tags_with_content(prompt)
# Insert new system message at the beginning of the list
self.chat_messages.insert(0, {"role": "system", "content": prompt})
return self
def add_user_chat(self, prompt: str, label: str = "latest") -> "AIFlow":
"""
Add a user chat message and get a response.
:param prompt: User chat message
:param label: Label for the context
:return: self
"""
if self.verbose:
print(prompt)
prompt = self.replace_tags_with_content(prompt)
self.context_map["latest_prompt"] = prompt
self.chat_messages.append({"role": "user", "content": prompt})
response = self.call_openai_chat_api(messages=self.chat_messages)
self.chat_messages.append({"role": "assistant", "content": response})
self.context_map["latest"] = response
self.context_map[label] = response
if self.verbose:
print(response)
if self.save_state_per_step:
self.save_internal_state()
return self
def filter_messages(
self, func: Optional[Callable[[List[Dict[str, str]]], List[Dict[str, str]]]]
) -> "AIFlow":
"""
Filter chat messages using a function.
:param func: Function to filter messages
:return: self
"""
if func is not None:
self.chat_messages = func(self.chat_messages)
return self
def reduce_messages_to_text(
self, func: Optional[Callable[[List[Dict[str, str]]], str]]
) -> "AIFlow":
"""
Reduce chat messages to text using a function.
:param func: Function to reduce messages
:return: self
"""
if func is not None:
self.context_map["latest"] = func(self.chat_messages)
print(self.context_map["latest"])
return self
#
# Simple completion
#
def generate_completion(self, prompt: str, label: str = "latest") -> "AIFlow":
if self.verbose:
print(prompt)
print()
prompt = self.replace_tags_with_content(prompt)
self.context_map["latest_prompt"] = prompt
messages = [{"role": "user", "content": prompt}]
response = self.call_openai_chat_api(messages=messages)
self._update_context(response, label)
if self.verbose:
print(response)
print()
if self.save_state_per_step:
self.save_internal_state()
return self
#
# Simple completion
#
def generate_json_completion(
self, prompt: str, label: str = "latest", schema=BaseModel
) -> "AIFlow":
if self.verbose:
print(prompt)
print()
prompt = self.replace_tags_with_content(prompt)
self.context_map["latest_prompt"] = prompt
messages = [{"role": "user", "content": prompt}]
response = self.call_openai_parse_api(messages=messages, schema=schema)
self._update_context(response.json(), label)
if self.verbose:
print(response)
print()
if self.save_state_per_step:
self.save_internal_state()
return self
def _update_context(self, response: str, label: str) -> None:
self.context_map["latest"] = response
self.context_map[label] = response
#
# OpenAI caller - completions
#
def call_openai_chat_api(self, messages: List[Dict[str, str]] = []) -> str:
"""
Call the OpenAI chat API with the given messages.
:param messages: List of messages
:return: Response from the API
"""
params = {
"model": self.model,
"max_tokens": self.max_tokens,
"temperature": self.temperature,
"messages": messages,
}
# Conditionally add response_format
if self.json_mode:
params["response_format"] = {"type": "json_object"}
try:
completion = self.client.chat.completions.create(**params)
self.update_token_usage(completion.usage)
return completion.choices[0].message.content
except OpenAIError as e:
print(f"OpenAI API error: {e}")
return "An error occurred with the OpenAI API."
except Exception as e:
print(f"Unexpected error: {e}")
return "An unexpected error occurred."
#
# OpenAI caller - json_schema
#
def call_openai_parse_api(
self, messages: List[Dict[str, str]] = [], schema=BaseModel
) -> str:
"""
Call the OpenAI chat API with the given messages and the schema to generate JONS
:param messages: List of messages
:param schema: Schema based on Pydantic BaseModel
:return: Response from the API
"""
params = {
"model": self.model,
"max_tokens": self.max_tokens,
"temperature": self.temperature,
"messages": messages,
"response_format": schema,
}
try:
completion = self.client.beta.chat.completions.parse(**params)
self.update_token_usage(completion.usage)
return completion.choices[0].message.parsed
except OpenAIError as e:
print(f"OpenAI API error: {e}")
return "An error occurred with the OpenAI API."
except Exception as e:
print(f"Unexpected error: {e}")
return "An unexpected error occurred."
#
# Completing context in prompts
#
def replace_tags_with_content(self, input_string: str = "") -> str:
"""
Replace tags in the input string with context content.
:param input_string: Input string with tags
:return: String with tags replaced by context content
"""
while True:
previous_string = input_string
for key, value in self.context_map.items():
input_string = input_string.replace(f"[{key}]", str(value))
# If no replacements were made in this iteration, break the loop
if input_string == previous_string:
break
return input_string
#
# Context functions
#
def copy_latest_to(self, label: str = "latest") -> "AIFlow":
"""
Copy the latest context to a specified label.
:param label: Label for the context
:return: self
"""
self.context_map[label] = self.context_map["latest"]
return self
def transform_context(
self, label: str = "latest", func: Callable[[str], str] = lambda x: x
) -> "AIFlow":
"""
Transform the context using a function.
:param label: Label for the context
:param func: Function to transform the context
:return: self
"""
if label != "latest" and label in self.context_map and func is not None:
self.context_map[label] = func(self.context_map[label])
return self
def set_context_of(self, content: str = "", label: str = "latest") -> "AIFlow":
"""
Set the context for a specified label.
:param content: Content to set
:param label: Label for the context
:return: self
"""
if label != "latest":
self.context_map[label] = content
return self
def delete_context(self, label: str = "latest") -> "AIFlow":
"""
Delete the context for a specified label.
:param label: Label for the context
:return: self
"""
if label != "latest" and label in self.context_map:
del self.context_map[label]
return self
def display_context_of(self, label: str = "latest") -> "AIFlow":
"""
Show the context for a specified label.
:param label: Label for the context
:return: self
"""
print(self.context_map[label])
return self
def display_context_keys(self) -> "AIFlow":
"""
Show all context keys.
:return: self
"""
keys_list = list(self.context_map.keys())
keys_str = ", ".join(keys_list)
print(keys_str)
return self
def return_context_keys(self) -> List[str]:
"""
Return all context keys.
:return: List of context keys
"""
return self.context_map.keys()
def load_to_context(self, filename: str, label: str = "latest_file") -> "AIFlow":
"""
Load content from a file into the context.
:param filename: Name of the file to load content from
:param label: Label for the context
:return: self
"""
try:
with open(filename, "r") as file:
content = file.read()
self.context_map[label] = content
except FileNotFoundError:
print(f"The file {filename} does not exist.")
except Exception as e:
print(f"An error occurred: {e}")
return self
def save_context_to_file(
self, label: str = "latest", filename: str = ""
) -> "AIFlow":
"""
Dump the context to a file.
:param label: Label for the context
:param filename: Name of the file to dump content to
:return: self
"""
if self.default_folder_for_output != "":
filename_2 = os.path.join(
self.default_folder_for_output, f"context_{label}.txt"
)
else:
filename_2 = f"context_{label}.txt"
if filename != "":
filename_2 = filename
with open(filename_2, "w") as file:
file.write(str(self.context_map[label]))
return self
def save_context_to_files(self) -> "AIFlow":
"""
Dump all contexts to files.
:return: self
"""
for key, value in self.context_map.items():
self.save_context_to_file(label=key)
return self
def load_multiple_context_from_file(self, filename: str = "") -> "AIFlow":
"""
Load a text file and process its content grouped by keywords.
Reads a file line by line, identifying sections where each keyword is
followed by content. The keyword lines end with a colon, and all lines
until the next keyword belong to that keyword's content. For each
keyword-content block found, it calls `set_context_of` with the keyword
as the label and the content as the section's text.
:param filename: Name of the file to load and process
:return: None
"""
with open(filename, "r") as file:
current_keyword = None
current_content = []
for line in file:
line = line.strip() # Remove whitespace and newline characters
# Check if the line is a keyword (contains a colon at the end)
if line.endswith(":"):
# Process the previous keyword-content block
if current_keyword:
# Join the collected lines and pass to the function
content = "\n".join(current_content)
self.set_context_of(label=current_keyword, content=content)
if self.verbose:
print(f"Stored context {current_keyword}")
# Set new keyword and reset content list
current_keyword = line[:-1] # Remove the colon
current_content = []
else:
# Accumulate lines under the current keyword
current_content.append(line)
# Process the last keyword-content block if it exists
if current_keyword:
content = "\n".join(current_content)
self.set_context_of(label=current_keyword, content=content)
if self.verbose:
print(f"Stored context {current_keyword}")
if self.save_state_per_step:
self.save_internal_state()
def save_context_to_markdown(
self, output_filename: str = "content.md"
) -> "AIFlow":
"""
Dump the context to a markdown file.
:param output_filename: Name of the markdown file
:return: self
"""
with open(output_filename, "w") as file:
for chapter, content in self.context_map.items():
file.write(f"# {chapter}\n\n")
file.write(f"{content}\n\n")
return self
def generate_heading_for_context(
self, label: str, prompt: str, replace: bool
) -> None:
"""
Generate a heading for a single context.
:param label: Label for the context
:param prompt: Prompt for generating the heading
:param replace: Whether to replace the existing heading
"""
content = self.context_map.get(label, "")
if not content:
return
heading_label = label + "_heading"
existing_heading = self.context_map.get(heading_label)
if replace or not existing_heading:
full_prompt = f"{prompt}{content}"
messages = [{"role": "user", "content": full_prompt}]
response = self.call_openai_chat_api(messages=messages)
self.set_context_of(label=heading_label, content=response)
if self.save_state_per_step:
self.save_internal_state()
def generate_headings_for_contexts(
self,
labels: List[str] = [],
prompt: str = "Generate a short 10 word summary of the following content:\n",
replace: bool = True,
) -> "AIFlow":
"""
Generate headings for multiple contexts.
:param labels: List of labels for the contexts
:param prompt: Prompt for generating the headings
:param replace: Whether to replace the existing headings
:return: self
"""
for label in labels:
self.generate_heading_for_context(label, prompt, replace)
return self
def save_context_to_docx(
self, output_filename: str, chapters_to_include: List[str] = []
) -> "AIFlow":
"""
Save the context to a DOCX file.
:param output_filename: Name of the DOCX file
:param chapters_to_include: List of chapters to include
:return: self
"""
document = Document()
for chapter, content in self.context_map.items():
if chapter in chapters_to_include:
heading_key = chapter + "_heading"
if heading_key in self.context_map:
document.add_heading(self.context_map[heading_key], level=1)
else:
document.add_heading(chapter, level=1)
document.add_paragraph(str(content))
document.save(output_filename)
return self
def save_context_to_html(
self, output_filename: str, chapters_to_include: List[str] = []
) -> "AIFlow":
"""
Save the context to an HTML file.
:param output_filename: Name of the HTML file
:param chapters_to_include: List of chapters to include
:return: self
"""
html_content = "<html><body>"
if chapters_to_include:
for chapter in chapters_to_include:
if chapter in self.context_map:
heading_key = chapter + "_heading"
if heading_key in self.context_map:
heading = self.context_map[heading_key]
else:
heading = chapter
html_content += f"<h1>{heading}</h1>"
html_content += markdown.markdown(str(self.context_map[chapter]))
else:
for chapter, content in self.context_map.items():
heading_key = chapter + "_heading"
if heading_key in self.context_map:
heading = self.context_map[heading_key]
else:
heading = chapter
html_content += f"<h1>{heading}</h1>"
html_content += markdown.markdown(str(content))
html_content += "</body></html>"
# Ensure the directory exists
os.makedirs(os.path.dirname(output_filename), exist_ok=True)
with open(output_filename, "w", encoding="utf-8") as f:
f.write(html_content)
return self
#
# Functions to support inclusion in other chat instances. Only implemented by returning strings
#
def get_latest_context_as_text(self) -> str:
"""
Get the latest context as text.
:return: Latest context as text
"""
return self.context_map["latest"]
def get_context_as_text(self, label: str = "latest") -> str:
"""
Get the context as text for a specified label.
:param label: Label for the context
:return: Context as text
"""
return self.context_map[label]
def get_context_as_json(self, label: str = "latest") -> str:
"""
Get the context as text for a specified label as json object
:param label: Label for the context
:return: Context as json
"""
return json.loads(self.context_map[label])
def get_reduced_chat_messages_as_text(
self, func: Optional[Callable[[List[Dict[str, str]]], str]]
) -> str:
"""
Get reduced chat messages as text using a function.
:param func: Function to reduce messages
:return: Reduced chat messages as text
"""
if func is not None:
return func(self.chat_messages)
return self
def display_latest_context_as_markdown(self) -> None:
"""
Display the latest context as markdown.
:return: None
"""
return display(Markdown(self.context_map["latest"]))
def display_context_as_markdown(self, label: str = "latest") -> None:
"""
Display the context as markdown for a specified label.
:param label: Label for the context
:return: None
"""
return display(Markdown(self.context_map[label]))
#
# Image generation
# dall-e-3 dall-e-2
# 1024x1024 512x512
# https://platform.openai.com/docs/api-reference/images/create
#
def generate_image(
self,
model: str = "dall-e-2",
style: str = "vivid",
response_format: str = "url",
prompt: str = "A white siamese cat",
size: str = "1024x1024",
quality: str = "standard",
n: int = 1,
label: str = "latest_image",
html: bool = False,
) -> "AIFlow":
"""
Generate an image.
:param model: Model to use for image generation
:param style: Style of the image
:param response_format: Format of the response (url or b64_json)
:param prompt: Prompt for image generation
:param size: Size of the image
:param quality: Quality of the image
:param n: Number of images to generate
:param label: Label for the generated image
:param html: Whether to return HTML for displaying the image
:return: self
"""
print(f"Generating image with prompt: {prompt}")
response = self.client.images.generate(
model=model,
prompt=prompt,
size=size,
quality=quality,
n=n,
response_format=response_format,
style=style,
)
image_data = response.data[0]
self.images_map[label] = (
image_data.url if response_format == "url" else image_data.b64_json
)
self.images_map["latest_image"] = self.images_map[label]
self.context_map["latest_image_prompt"] = prompt
self.context_map["latest_revised_image_prompt"] = image_data.revised_prompt
if html:
return HTML(f'<img src="{self.images_map[label]}" />')
return self
def save_image_to_file(
self, label: str = "latest_image", filename: str = ""
) -> "AIFlow":
"""
Save the generated image to a file.
:param label: Label for the generated image
:param filename: Name of the file to save the image
:return: self
"""
"""
Save the generated image to a file.
:param label: Label for the generated image
:param filename: Name of the file to save the image
:return: self
"""
output_filename = filename if filename else f"image_{label}.jpg"
if not output_filename.lower().endswith(".jpg"):
output_filename += ".jpg"
if self.verbose:
print(f"Saving {label} to {output_filename}")
if label in self.images_map:
urllib.request.urlretrieve(self.images_map[label], output_filename)
return self
#
# Vision model
# https://platform.openai.com/docs/guides/vision
#
def analyze_image(
self,
image: str = "",
prompt: str = "What's in this image?",
model: str = "gpt-4o",
label: str = "latest",
detail: str = "low",
max_tokens: int = 300,
) -> "AIFlow":
"""
Analyze an image.