generated from ks6088ts/template-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
582 lines (475 loc) · 14.8 KB
/
main.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
import logging
import typer
from dotenv import load_dotenv
assert load_dotenv(override=True), "Failed to load .env file"
logger = logging.getLogger(__name__)
app = typer.Typer()
def set_verbosity(verbose: bool):
if verbose:
logging.basicConfig(level=logging.DEBUG)
# ---
# agents
# ---
@app.command(
help="Chatbot with tools",
)
def agents_chatbot_with_tools_run(
verbose: bool = False,
):
set_verbosity(verbose)
from workshop_llm_agents.agents.chatbot_with_tools import graph
config = {
"configurable": {
"thread_id": "1",
},
}
while True:
exit_code = "q"
query = input(f"Enter a query(type '{exit_code}' to exit): ")
if query == exit_code:
break
events = graph.stream(
input={
"messages": [
("user", query),
]
},
config=config,
stream_mode="values",
)
for event in events:
if "messages" in event:
event["messages"][-1].pretty_print()
@app.command(
help="Export the graph to a PNG file",
)
def agents_chatbot_with_tools_export(
png: str = None,
verbose: bool = False,
):
set_verbosity(verbose)
from workshop_llm_agents.agents.chatbot_with_tools import graph
print(graph.get_graph().draw_mermaid())
if png:
graph.get_graph().draw_mermaid_png(
output_file_path=png,
)
@app.command(
help="Run documentation agent",
)
def agents_documentation_run(
user_request: str = "スマートフォン向けの健康管理アプリを開発したい",
k: int = 3,
verbose: bool = True,
):
set_verbosity(verbose)
from workshop_llm_agents.agents.documentation_agent import DocumentationAgent
from workshop_llm_agents.llms.azure_openai import AzureOpenAIWrapper
azure_openai_wrapper = AzureOpenAIWrapper()
llm = azure_openai_wrapper.get_azure_chat_openai()
agent = DocumentationAgent(llm=llm, k=k)
final_output = agent.run(user_request=user_request)
print(final_output)
@app.command(
help="Run single path plan generation agent",
)
def agents_single_path_plan_generation_run(
query: str = "スマートフォン向けの健康管理アプリを開発したい",
verbose: bool = True,
):
set_verbosity(verbose)
from workshop_llm_agents.agents.single_path_plan_generation_agent import SinglePathPlanGenerationAgent
from workshop_llm_agents.llms.azure_openai import AzureOpenAIWrapper
from workshop_llm_agents.tools.bing_search import BingSearchWrapper
agent = SinglePathPlanGenerationAgent(
llm=AzureOpenAIWrapper().get_azure_chat_openai(),
tools=[
BingSearchWrapper().get_bing_search_tool(),
],
)
final_output = agent.run(query=query)
print(final_output)
@app.command(
help="Run summarize agent",
)
def agents_summarize_run(
web_path: str = "https://learn.microsoft.com/ja-jp/azure/ai-services/openai/overview",
png=None,
verbose: bool = True,
):
set_verbosity(verbose)
import asyncio
from langchain_community.document_loaders import WebBaseLoader
from langchain_text_splitters import CharacterTextSplitter
from workshop_llm_agents.agents.summarize_agent import SummarizeAgent
from workshop_llm_agents.llms.azure_openai import AzureOpenAIWrapper
azure_openai_wrapper = AzureOpenAIWrapper()
llm = azure_openai_wrapper.get_azure_chat_openai()
agent = SummarizeAgent(
llm=llm,
)
if png:
try:
image_bytes = agent.mermaid_png(output_file_path=png)
with open(png, "wb") as f:
f.write(image_bytes)
except Exception as e:
logger.error(f"failing to save PNG: {e}")
loader = WebBaseLoader(
web_path=web_path,
)
docs = loader.load()
text_splitter = CharacterTextSplitter.from_tiktoken_encoder(
chunk_size=1000,
chunk_overlap=0,
)
chunks = text_splitter.split_documents(docs)
response = asyncio.run(agent.run(docs=chunks))
print(f"final output: {response}")
@app.command(
help="Run tools agent",
)
def agents_tools_run(
query: str = "最近の京都のニュース",
verbose: bool = True,
):
set_verbosity(verbose)
from workshop_llm_agents.agents.tools_agent import ToolsAgent
from workshop_llm_agents.llms.azure_openai import AzureOpenAIWrapper
from workshop_llm_agents.tools.bing_search import BingSearchWrapper
azure_openai_wrapper = AzureOpenAIWrapper()
llm = azure_openai_wrapper.get_azure_chat_openai()
agent = ToolsAgent(
llm=llm,
tools=[
BingSearchWrapper().get_bing_search_tool(),
],
)
response = agent.run(query=query, thread_id="1")
print(f"final output: {response}")
@app.command(
help="Export the tools agent graph to a PNG file",
)
def agents_tools_export(
png: str = None,
verbose: bool = False,
):
set_verbosity(verbose)
from workshop_llm_agents.agents.chatbot_with_tools import graph
print(graph.get_graph().draw_mermaid())
if png:
graph.get_graph().draw_mermaid_png(
output_file_path=png,
)
# ---
# llms
# ---
@app.command(
help="Chat with Azure OpenAI",
)
def llms_azure_openai_chat(
message: str = "What is the capital of Japan?",
verbose: bool = False,
):
set_verbosity(verbose)
import json
from workshop_llm_agents.llms.azure_openai import AzureOpenAIWrapper
azure_openai_wrapper = AzureOpenAIWrapper()
llm = azure_openai_wrapper.get_azure_chat_openai()
response = llm.invoke(input=message)
print(
json.dumps(
response.model_dump(),
indent=2,
)
)
@app.command(
help="Embed query with Azure OpenAI",
)
def llms_azure_openai_embeddings(
message: str = "What is the capital of Japan?",
verbose: bool = False,
):
set_verbosity(verbose)
from workshop_llm_agents.llms.azure_openai import AzureOpenAIWrapper
azure_openai_wrapper = AzureOpenAIWrapper()
embeddings = azure_openai_wrapper.get_azure_openai_embeddings()
embedding = embeddings.embed_query(message)
print(f"Dimensions: {len(embedding)}")
logger.info(embedding)
@app.command(
help="Chat with Ollama model",
)
def llms_ollama_chat(
message: str = "What is the capital of Japan?",
verbose: bool = False,
):
set_verbosity(verbose)
import json
from workshop_llm_agents.llms.ollama import OllamaWrapper
wrapper = OllamaWrapper()
llm = wrapper.get_chat_ollama()
response = llm.invoke(input=message)
print(
json.dumps(
response.model_dump(),
indent=2,
ensure_ascii=False,
)
)
# ---
# tasks
# ---
@app.command(
help="Run the image labeler task",
)
def tasks_image_labeler(
file: str = "./docs/images/workshop-llm-agents.png",
verbose: bool = False,
):
set_verbosity(verbose)
import base64
from workshop_llm_agents.llms.azure_openai import AzureOpenAIWrapper
from workshop_llm_agents.tasks.image_labeler import ImageLabeler, LabelingResult
try:
with open(file, "rb") as f:
encoded_image = base64.b64encode(f.read()).decode()
except Exception as e:
print(e)
exit(1)
llm = AzureOpenAIWrapper().get_azure_chat_openai()
task = ImageLabeler(llm=llm)
result: LabelingResult = task.run(encoded_image=encoded_image)
print(f"Labels: {result.labels}")
@app.command(
help="Run the passive goal creator task",
)
def tasks_passive_goal_creator(
query: str = "I want to learn how to cook",
verbose: bool = False,
):
set_verbosity(verbose)
from workshop_llm_agents.llms.azure_openai import AzureOpenAIWrapper
from workshop_llm_agents.tasks.passive_goal_creator import Goal, PassiveGoalCreator
llm = AzureOpenAIWrapper().get_azure_chat_openai()
task = PassiveGoalCreator(llm=llm)
result: Goal = task.run(query=query)
print(result.text)
@app.command(
help="Run the prompt optimizer task",
)
def tasks_prompt_optimizer(
query: str = "I want to learn how to cook",
verbose: bool = False,
):
set_verbosity(verbose)
from workshop_llm_agents.llms.azure_openai import AzureOpenAIWrapper
from workshop_llm_agents.tasks.prompt_optimizer import OptimizedGoal, PromptOptimizer
llm = AzureOpenAIWrapper().get_azure_chat_openai()
task = PromptOptimizer(llm=llm)
result: OptimizedGoal = task.run(query=query)
print(result.text)
@app.command(
help="Run the query decomposer task",
)
def tasks_query_decomposer(
query: str = "I want to learn how to cook",
verbose: bool = False,
):
set_verbosity(verbose)
from workshop_llm_agents.llms.azure_openai import AzureOpenAIWrapper
from workshop_llm_agents.tasks.query_decomposer import DecomposedTasks, QueryDecomposer
llm = AzureOpenAIWrapper().get_azure_chat_openai()
task = QueryDecomposer(llm=llm)
result: DecomposedTasks = task.run(query=query)
print(result)
@app.command(
help="Run the response optimizer task",
)
def tasks_response_optimizer(
query: str = "I want to learn how to cook",
verbose: bool = False,
):
set_verbosity(verbose)
from workshop_llm_agents.llms.azure_openai import AzureOpenAIWrapper
from workshop_llm_agents.tasks.response_optimizer import ResponseOptimizer
llm = AzureOpenAIWrapper().get_azure_chat_openai()
task = ResponseOptimizer(llm=llm)
result = task.run(query=query)
print(result)
@app.command(
help="Run the result aggregator task",
)
def tasks_result_aggregator(
query: str = "I want to learn how to cook",
verbose: bool = False,
):
set_verbosity(verbose)
from workshop_llm_agents.llms.azure_openai import AzureOpenAIWrapper
from workshop_llm_agents.tasks.result_aggregator import ResultAggregator
llm = AzureOpenAIWrapper().get_azure_chat_openai()
task = ResultAggregator(llm=llm)
result = task.run(
query=query,
response_definition="Provide a summary of the search results",
results=[
"Learn how to use a knife",
"Practice cooking rice",
"Learn how to make a salad",
],
)
print(result)
@app.command(
help="Run the scoring evaluator task",
)
def tasks_scoring_evaluator(
query: str = "Something wrong with my computer",
verbose: bool = False,
):
set_verbosity(verbose)
from workshop_llm_agents.llms.azure_openai import AzureOpenAIWrapper
from workshop_llm_agents.tasks.scoring_evaluator import ScoringEvaluator
llm = AzureOpenAIWrapper().get_azure_chat_openai()
task = ScoringEvaluator(llm=llm)
result = task.run(query=query)
print(result)
@app.command(
help="Run the task executor",
)
def tasks_task_executor(
task: str = "What's the weather in Tokyo today?",
verbose: bool = False,
):
set_verbosity(verbose)
from workshop_llm_agents.llms.azure_openai import AzureOpenAIWrapper
from workshop_llm_agents.tasks.task_executor import TaskExecutor
from workshop_llm_agents.tools.bing_search import BingSearchWrapper
llm = AzureOpenAIWrapper().get_azure_chat_openai()
task_executor = TaskExecutor(
llm=llm,
tools=[
BingSearchWrapper().get_bing_search_tool(),
],
)
result = task_executor.run(task=task)
print(result)
# ---
# tools
# ---
@app.command(
help="Search Bing",
)
def tools_bing_search(
query: str = "Microsoft",
verbose: bool = False,
):
set_verbosity(verbose)
import json
from workshop_llm_agents.tools.bing_search import BingSearchWrapper
wrapper = BingSearchWrapper()
tool = wrapper.get_bing_search_tool()
response = tool.invoke(input=query)
response = json.loads(response.replace("'", '"'))
print(
json.dumps(
response,
indent=2,
ensure_ascii=False,
)
)
@app.command(
help="Search CosmosDB",
)
def tools_cosmosdb_search(
query: str = "Microsoft",
verbose: bool = False,
):
set_verbosity(verbose)
import json
from workshop_llm_agents.tools.cosmosdb_search import CosmosDBSearchWrapper
wrapper = CosmosDBSearchWrapper()
tool = wrapper.get_cosmosdb_search_tool()
response_str = tool.invoke(input=query)
response_json = json.loads(response_str.replace("'", '"'))
print(
json.dumps(
response_json,
indent=2,
ensure_ascii=False,
)
)
# ---
# vector_stores
# ---
@app.command(
help="Insert data into the CosmosDB",
)
def vector_stores_cosmosdb_insert_data(
pdf_url: str = "https://www.maff.go.jp/j/zyukyu/zikyu_ritu/attach/pdf/012-9.pdf",
chunk_size: int = 500,
chunk_overlap: int = 100,
verbose: bool = False,
):
set_verbosity(verbose)
from langchain_community.document_loaders import PyMuPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from workshop_llm_agents.llms.azure_openai import AzureOpenAIWrapper
from workshop_llm_agents.vector_stores.cosmosdb import CosmosDBWrapper
cosmosdb_wrapper = CosmosDBWrapper()
vector_store = cosmosdb_wrapper.get_azure_cosmos_db_no_sql_vector_search(
embedding=AzureOpenAIWrapper().get_azure_openai_embeddings(),
)
# Load the PDF
loader = PyMuPDFLoader(file_path=pdf_url)
data = loader.load()
# Split the text into chunks
docs = RecursiveCharacterTextSplitter(
chunk_size=chunk_size,
chunk_overlap=chunk_overlap,
).split_documents(data)
try:
vector_store.add_documents(docs)
except Exception as e:
logger.error(f"error: {e}")
@app.command(
help="Query data from the CosmosDB",
)
def vector_stores_cosmosdb_query_data(
query: str = "食料自給率の長期的推移",
verbose: bool = False,
):
set_verbosity(verbose)
import json
from workshop_llm_agents.llms.azure_openai import AzureOpenAIWrapper
from workshop_llm_agents.vector_stores.cosmosdb import CosmosDBWrapper
cosmosdb_wrapper = CosmosDBWrapper()
vector_store = cosmosdb_wrapper.get_azure_cosmos_db_no_sql_vector_search(
embedding=AzureOpenAIWrapper().get_azure_openai_embeddings(),
)
documents = vector_store.similarity_search(
query=query,
)
for idx, document in enumerate(documents):
print(f"Document {idx + 1} ---")
print(
json.dumps(
document.model_dump(),
indent=2,
ensure_ascii=False,
)
)
# ---
# streamlit
# ---
@app.command(
help="NOTE: To run the Streamlit app run `$ poetry run streamlit run main.py streamlit-app`",
)
def streamlit_app(
verbose: bool = True,
):
set_verbosity(verbose)
import streamlit as st
st.title("Code samples for Streamlit")
st.info("Select a code sample from the sidebar to run it")
if __name__ == "__main__":
app()