Spaces:
Sleeping
Sleeping
update
Browse files- agent.py +9 -4
- app.py +37 -3
- app_playground.ipynb +580 -5
- playground_api.ipynb +0 -0
- requirements.txt +3 -1
agent.py
CHANGED
|
@@ -12,8 +12,11 @@ from langgraph.prebuilt import ToolNode
|
|
| 12 |
from typing_extensions import TypedDict, Annotated
|
| 13 |
|
| 14 |
|
|
|
|
| 15 |
class State(TypedDict):
|
| 16 |
messages: Annotated[list, add_messages]
|
|
|
|
|
|
|
| 17 |
aggregate: Annotated[list, operator.add]
|
| 18 |
# graph_state: str
|
| 19 |
|
|
@@ -73,12 +76,16 @@ def get_graph(llm):
|
|
| 73 |
|
| 74 |
return serialized
|
| 75 |
|
|
|
|
|
|
|
| 76 |
tools = [retrieve, online_search]
|
| 77 |
tool_node = ToolNode(tools)
|
| 78 |
llm_with_tools = llm.bind_tools(tools)
|
| 79 |
|
| 80 |
def make_plan(state: State):
|
|
|
|
| 81 |
print("\n-------------------- Starting to create a plan --------------------\n")
|
|
|
|
| 82 |
# get all messages from the state
|
| 83 |
messages = state["messages"]
|
| 84 |
# append planning message
|
|
@@ -103,10 +110,7 @@ def get_graph(llm):
|
|
| 103 |
prompt_answer = prompt_template.invoke(messages)
|
| 104 |
# invoke LLM
|
| 105 |
response = llm_with_tools.invoke(prompt_answer)
|
| 106 |
-
print("
|
| 107 |
-
print("Agent has made a decision:\n",response, response.content, response.tool_calls)
|
| 108 |
-
print("Type von der Antwort: ",type(response))
|
| 109 |
-
|
| 110 |
print("Waiting for 4 seconds...")
|
| 111 |
time.sleep(4)
|
| 112 |
|
|
@@ -121,6 +125,7 @@ def get_graph(llm):
|
|
| 121 |
prompt_answer = prompt_template.invoke(messages)
|
| 122 |
# invoke LLM
|
| 123 |
response = llm.invoke(prompt_answer)
|
|
|
|
| 124 |
return {"messages": [response], "aggregate": ["Answer"]}
|
| 125 |
|
| 126 |
def should_continue(state: State):
|
|
|
|
| 12 |
from typing_extensions import TypedDict, Annotated
|
| 13 |
|
| 14 |
|
| 15 |
+
|
| 16 |
class State(TypedDict):
|
| 17 |
messages: Annotated[list, add_messages]
|
| 18 |
+
content_type: str
|
| 19 |
+
content: str
|
| 20 |
aggregate: Annotated[list, operator.add]
|
| 21 |
# graph_state: str
|
| 22 |
|
|
|
|
| 76 |
|
| 77 |
return serialized
|
| 78 |
|
| 79 |
+
|
| 80 |
+
|
| 81 |
tools = [retrieve, online_search]
|
| 82 |
tool_node = ToolNode(tools)
|
| 83 |
llm_with_tools = llm.bind_tools(tools)
|
| 84 |
|
| 85 |
def make_plan(state: State):
|
| 86 |
+
|
| 87 |
print("\n-------------------- Starting to create a plan --------------------\n")
|
| 88 |
+
print("Content is: ", state["content_type"])
|
| 89 |
# get all messages from the state
|
| 90 |
messages = state["messages"]
|
| 91 |
# append planning message
|
|
|
|
| 110 |
prompt_answer = prompt_template.invoke(messages)
|
| 111 |
# invoke LLM
|
| 112 |
response = llm_with_tools.invoke(prompt_answer)
|
| 113 |
+
print("Agent has made a decision:\n", response.content, response.tool_calls)
|
|
|
|
|
|
|
|
|
|
| 114 |
print("Waiting for 4 seconds...")
|
| 115 |
time.sleep(4)
|
| 116 |
|
|
|
|
| 125 |
prompt_answer = prompt_template.invoke(messages)
|
| 126 |
# invoke LLM
|
| 127 |
response = llm.invoke(prompt_answer)
|
| 128 |
+
print("The final answer is: ", response.content)
|
| 129 |
return {"messages": [response], "aggregate": ["Answer"]}
|
| 130 |
|
| 131 |
def should_continue(state: State):
|
app.py
CHANGED
|
@@ -6,8 +6,10 @@ import pandas as pd
|
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
import json
|
| 8 |
import time
|
|
|
|
| 9 |
|
| 10 |
from agent import *
|
|
|
|
| 11 |
|
| 12 |
# (Keep Constants as is)
|
| 13 |
# --- Constants ---
|
|
@@ -22,9 +24,30 @@ class BasicAgent:
|
|
| 22 |
self.graph = get_graph(self.llm)
|
| 23 |
|
| 24 |
print("BasicAgent initialized.")
|
| 25 |
-
def __call__(self, question: str) -> str:
|
| 26 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
print("Agents answert to the question: ",response["messages"][-1].content)
|
| 29 |
return response["messages"][-1].content
|
| 30 |
|
|
@@ -91,11 +114,22 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 91 |
time.sleep(10)
|
| 92 |
task_id = item.get("task_id")
|
| 93 |
question_text = item.get("question")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
if not task_id or question_text is None:
|
| 95 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 96 |
continue
|
| 97 |
try:
|
| 98 |
-
submitted_answer = agent(question_text)
|
| 99 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 100 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 101 |
except Exception as e:
|
|
|
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
import json
|
| 8 |
import time
|
| 9 |
+
import base64
|
| 10 |
|
| 11 |
from agent import *
|
| 12 |
+
from app_playground import response
|
| 13 |
|
| 14 |
# (Keep Constants as is)
|
| 15 |
# --- Constants ---
|
|
|
|
| 24 |
self.graph = get_graph(self.llm)
|
| 25 |
|
| 26 |
print("BasicAgent initialized.")
|
| 27 |
+
def __call__(self, question: str, content=None, content_type= None) -> str:
|
| 28 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 29 |
+
if content_type == "image/png":
|
| 30 |
+
image = base64.b64encode(content).decode("utf-8")
|
| 31 |
+
|
| 32 |
+
message = {
|
| 33 |
+
"role": "user",
|
| 34 |
+
"content": [
|
| 35 |
+
{
|
| 36 |
+
"type": "text",
|
| 37 |
+
"text": "What do you see in the image?",
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"type": "image",
|
| 41 |
+
"source_type": "base64",
|
| 42 |
+
"data": image,
|
| 43 |
+
"mime_type": "image/png",
|
| 44 |
+
},
|
| 45 |
+
],
|
| 46 |
+
}
|
| 47 |
+
response = self.graph.invoke(message)
|
| 48 |
+
else :
|
| 49 |
+
response = self.graph.invoke({"messages": [HumanMessage(content=question),]})
|
| 50 |
+
|
| 51 |
print("Agents answert to the question: ",response["messages"][-1].content)
|
| 52 |
return response["messages"][-1].content
|
| 53 |
|
|
|
|
| 114 |
time.sleep(10)
|
| 115 |
task_id = item.get("task_id")
|
| 116 |
question_text = item.get("question")
|
| 117 |
+
|
| 118 |
+
# Check if there is a additional file to be used
|
| 119 |
+
if item.get("file_name"):
|
| 120 |
+
file = requests.get(f"{api_url}/files/{task_id}")
|
| 121 |
+
content_type = file.headers.get("Content-Type")
|
| 122 |
+
|
| 123 |
+
if content_type == "image/png":
|
| 124 |
+
content = file.content
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
if not task_id or question_text is None:
|
| 129 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 130 |
continue
|
| 131 |
try:
|
| 132 |
+
submitted_answer = agent(question_text, content, content_type)
|
| 133 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 134 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 135 |
except Exception as e:
|
app_playground.ipynb
CHANGED
|
@@ -6,8 +6,8 @@
|
|
| 6 |
"metadata": {
|
| 7 |
"collapsed": true,
|
| 8 |
"ExecuteTime": {
|
| 9 |
-
"end_time": "2025-04-
|
| 10 |
-
"start_time": "2025-04-
|
| 11 |
}
|
| 12 |
},
|
| 13 |
"source": [
|
|
@@ -28,8 +28,8 @@
|
|
| 28 |
{
|
| 29 |
"metadata": {
|
| 30 |
"ExecuteTime": {
|
| 31 |
-
"end_time": "2025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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
{
|
| 458 |
"metadata": {},
|
| 459 |
"cell_type": "code",
|
| 460 |
"outputs": [],
|
| 461 |
"execution_count": null,
|
| 462 |
"source": "",
|
| 463 |
-
"id": "
|
| 464 |
}
|
| 465 |
],
|
| 466 |
"metadata": {
|
|
|
|
| 6 |
"metadata": {
|
| 7 |
"collapsed": true,
|
| 8 |
"ExecuteTime": {
|
| 9 |
+
"end_time": "2025-04-28T11:49:33.800394Z",
|
| 10 |
+
"start_time": "2025-04-28T11:49:27.768470Z"
|
| 11 |
}
|
| 12 |
},
|
| 13 |
"source": [
|
|
|
|
| 28 |
{
|
| 29 |
"metadata": {
|
| 30 |
"ExecuteTime": {
|
| 31 |
+
"end_time": "2025-04-28T11:24:12.828295Z",
|
| 32 |
+
"start_time": "2025-04-28T11:24:01.696627Z"
|
| 33 |
}
|
| 34 |
},
|
| 35 |
"cell_type": "code",
|
|
|
|
| 454 |
],
|
| 455 |
"execution_count": 8
|
| 456 |
},
|
| 457 |
+
{
|
| 458 |
+
"metadata": {
|
| 459 |
+
"ExecuteTime": {
|
| 460 |
+
"end_time": "2025-04-28T09:59:31.346154Z",
|
| 461 |
+
"start_time": "2025-04-28T09:59:26.018247Z"
|
| 462 |
+
}
|
| 463 |
+
},
|
| 464 |
+
"cell_type": "code",
|
| 465 |
+
"source": [
|
| 466 |
+
"import requests\n",
|
| 467 |
+
"url = \"https://agents-course-unit4-scoring.hf.space\"\n",
|
| 468 |
+
"task_id = \"cca530fc-4052-43b2-b130-b30968d8aa44\"\n",
|
| 469 |
+
"\n",
|
| 470 |
+
"\n",
|
| 471 |
+
"#res = graph.invoke({\"messages\": [HumanMessage(content=\"How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia.\"),]})\n",
|
| 472 |
+
"\n",
|
| 473 |
+
"res = graph.invoke({\n",
|
| 474 |
+
" \"role\": \"user\",\n",
|
| 475 |
+
" \"content\": [\n",
|
| 476 |
+
" {\"type\": \"text\", \"text\": \"Review the chess position provided in the image. It is black's turn. Provide the correct next move for black which guarantees a win. Please provide your response in algebraic notation.\"},\n",
|
| 477 |
+
" {\"type\": \"image\", \"source_type\": \"url\", \"url\": \"https://agents-course-unit4-scoring.hf.space/files/7bd855d8-463d-4ed5-93ca-5fe35145f733\"},\n",
|
| 478 |
+
" ],\n",
|
| 479 |
+
"})\n",
|
| 480 |
+
"\n"
|
| 481 |
+
],
|
| 482 |
+
"id": "5d4dada891453bed",
|
| 483 |
+
"outputs": [
|
| 484 |
+
{
|
| 485 |
+
"name": "stdout",
|
| 486 |
+
"output_type": "stream",
|
| 487 |
+
"text": [
|
| 488 |
+
"\n",
|
| 489 |
+
"-------------------- Starting to create a plan --------------------\n",
|
| 490 |
+
"\n",
|
| 491 |
+
"The plan is: I am unable to assist with that request.\n",
|
| 492 |
+
"\n",
|
| 493 |
+
"-------------------- Agent has been called -----------------------------------\n",
|
| 494 |
+
"\n",
|
| 495 |
+
"Agent has made a decision:\n",
|
| 496 |
+
" I am unable to assist with that request. []\n",
|
| 497 |
+
"Waiting for 4 seconds...\n",
|
| 498 |
+
"\n",
|
| 499 |
+
"-------------------- Decision of forwarding has been made --------------------\n",
|
| 500 |
+
"\n",
|
| 501 |
+
"This is round: 2\n",
|
| 502 |
+
"The last message is: content='I am unable to assist with that request.' additional_kwargs={} response_metadata={'prompt_feedback': {'block_reason': 0, 'safety_ratings': []}, 'finish_reason': 'STOP', 'model_name': 'gemini-2.0-flash', 'safety_ratings': []} id='run-ebfd867d-672e-4351-b60d-d4a692d44d10-0' usage_metadata={'input_tokens': 269, 'output_tokens': 10, 'total_tokens': 279, 'input_token_details': {'cache_read': 0}}\n",
|
| 503 |
+
"The final answer is: I am unable to assist with that request.\n"
|
| 504 |
+
]
|
| 505 |
+
}
|
| 506 |
+
],
|
| 507 |
+
"execution_count": 4
|
| 508 |
+
},
|
| 509 |
+
{
|
| 510 |
+
"metadata": {
|
| 511 |
+
"ExecuteTime": {
|
| 512 |
+
"end_time": "2025-04-28T10:01:39.769344Z",
|
| 513 |
+
"start_time": "2025-04-28T10:01:39.704253Z"
|
| 514 |
+
}
|
| 515 |
+
},
|
| 516 |
+
"cell_type": "code",
|
| 517 |
+
"source": [
|
| 518 |
+
"message = {\n",
|
| 519 |
+
" \"role\": \"user\",\n",
|
| 520 |
+
" \"content\": [\n",
|
| 521 |
+
" {\n",
|
| 522 |
+
" \"type\": \"text\",\n",
|
| 523 |
+
" \"text\": \"Describe the weather in this image:\",\n",
|
| 524 |
+
" },\n",
|
| 525 |
+
" {\n",
|
| 526 |
+
" \"type\": \"image\",\n",
|
| 527 |
+
" \"source_type\": \"url\",\n",
|
| 528 |
+
" \"url\": image_url,\n",
|
| 529 |
+
" },\n",
|
| 530 |
+
" ],\n",
|
| 531 |
+
"}\n",
|
| 532 |
+
"response = llm.invoke([message])\n",
|
| 533 |
+
"print(response.text())"
|
| 534 |
+
],
|
| 535 |
+
"id": "9d8999f9f946b596",
|
| 536 |
+
"outputs": [
|
| 537 |
+
{
|
| 538 |
+
"ename": "ValueError",
|
| 539 |
+
"evalue": "Invalid input type <class 'dict'>. Must be a PromptValue, str, or list of BaseMessages.",
|
| 540 |
+
"output_type": "error",
|
| 541 |
+
"traceback": [
|
| 542 |
+
"\u001B[31m---------------------------------------------------------------------------\u001B[39m",
|
| 543 |
+
"\u001B[31mValueError\u001B[39m Traceback (most recent call last)",
|
| 544 |
+
"\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[9]\u001B[39m\u001B[32m, line 1\u001B[39m\n\u001B[32m----> \u001B[39m\u001B[32m1\u001B[39m \u001B[43mllm\u001B[49m\u001B[43m.\u001B[49m\u001B[43minvoke\u001B[49m\u001B[43m(\u001B[49m\u001B[43m{\u001B[49m\n\u001B[32m 2\u001B[39m \u001B[43m \u001B[49m\u001B[33;43m\"\u001B[39;49m\u001B[33;43mrole\u001B[39;49m\u001B[33;43m\"\u001B[39;49m\u001B[43m:\u001B[49m\u001B[43m \u001B[49m\u001B[33;43m\"\u001B[39;49m\u001B[33;43muser\u001B[39;49m\u001B[33;43m\"\u001B[39;49m\u001B[43m,\u001B[49m\n\u001B[32m 3\u001B[39m \u001B[43m \u001B[49m\u001B[33;43m\"\u001B[39;49m\u001B[33;43mcontent\u001B[39;49m\u001B[33;43m\"\u001B[39;49m\u001B[43m:\u001B[49m\u001B[43m \u001B[49m\u001B[33;43m\"\u001B[39;49m\u001B[33;43mHello!\u001B[39;49m\u001B[33;43m\"\u001B[39;49m\u001B[43m,\u001B[49m\n\u001B[32m 4\u001B[39m \u001B[43m}\u001B[49m\u001B[43m)\u001B[49m.content\n",
|
| 545 |
+
"\u001B[36mFile \u001B[39m\u001B[32m~\\PycharmProjects\\Final_Assignment\\.venv\\Lib\\site-packages\\langchain_google_genai\\chat_models.py:1175\u001B[39m, in \u001B[36mChatGoogleGenerativeAI.invoke\u001B[39m\u001B[34m(self, input, config, code_execution, stop, **kwargs)\u001B[39m\n\u001B[32m 1170\u001B[39m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[32m 1171\u001B[39m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\n\u001B[32m 1172\u001B[39m \u001B[33m\"\u001B[39m\u001B[33mTools are already defined.\u001B[39m\u001B[33m\"\u001B[39m \u001B[33m\"\u001B[39m\u001B[33mcode_execution tool can\u001B[39m\u001B[33m'\u001B[39m\u001B[33mt be defined\u001B[39m\u001B[33m\"\u001B[39m\n\u001B[32m 1173\u001B[39m )\n\u001B[32m-> \u001B[39m\u001B[32m1175\u001B[39m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28;43msuper\u001B[39;49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\u001B[43m.\u001B[49m\u001B[43minvoke\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;28;43minput\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mconfig\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mstop\u001B[49m\u001B[43m=\u001B[49m\u001B[43mstop\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43m*\u001B[49m\u001B[43m*\u001B[49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\n",
|
| 546 |
+
"\u001B[36mFile \u001B[39m\u001B[32m~\\PycharmProjects\\Final_Assignment\\.venv\\Lib\\site-packages\\langchain_core\\language_models\\chat_models.py:370\u001B[39m, in \u001B[36mBaseChatModel.invoke\u001B[39m\u001B[34m(self, input, config, stop, **kwargs)\u001B[39m\n\u001B[32m 357\u001B[39m \u001B[38;5;129m@override\u001B[39m\n\u001B[32m 358\u001B[39m \u001B[38;5;28;01mdef\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[34minvoke\u001B[39m(\n\u001B[32m 359\u001B[39m \u001B[38;5;28mself\u001B[39m,\n\u001B[32m (...)\u001B[39m\u001B[32m 364\u001B[39m **kwargs: Any,\n\u001B[32m 365\u001B[39m ) -> BaseMessage:\n\u001B[32m 366\u001B[39m config = ensure_config(config)\n\u001B[32m 367\u001B[39m \u001B[38;5;28;01mreturn\u001B[39;00m cast(\n\u001B[32m 368\u001B[39m \u001B[33m\"\u001B[39m\u001B[33mChatGeneration\u001B[39m\u001B[33m\"\u001B[39m,\n\u001B[32m 369\u001B[39m \u001B[38;5;28mself\u001B[39m.generate_prompt(\n\u001B[32m--> \u001B[39m\u001B[32m370\u001B[39m [\u001B[38;5;28;43mself\u001B[39;49m\u001B[43m.\u001B[49m\u001B[43m_convert_input\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;28;43minput\u001B[39;49m\u001B[43m)\u001B[49m],\n\u001B[32m 371\u001B[39m stop=stop,\n\u001B[32m 372\u001B[39m callbacks=config.get(\u001B[33m\"\u001B[39m\u001B[33mcallbacks\u001B[39m\u001B[33m\"\u001B[39m),\n\u001B[32m 373\u001B[39m tags=config.get(\u001B[33m\"\u001B[39m\u001B[33mtags\u001B[39m\u001B[33m\"\u001B[39m),\n\u001B[32m 374\u001B[39m metadata=config.get(\u001B[33m\"\u001B[39m\u001B[33mmetadata\u001B[39m\u001B[33m\"\u001B[39m),\n\u001B[32m 375\u001B[39m run_name=config.get(\u001B[33m\"\u001B[39m\u001B[33mrun_name\u001B[39m\u001B[33m\"\u001B[39m),\n\u001B[32m 376\u001B[39m run_id=config.pop(\u001B[33m\"\u001B[39m\u001B[33mrun_id\u001B[39m\u001B[33m\"\u001B[39m, \u001B[38;5;28;01mNone\u001B[39;00m),\n\u001B[32m 377\u001B[39m **kwargs,\n\u001B[32m 378\u001B[39m ).generations[\u001B[32m0\u001B[39m][\u001B[32m0\u001B[39m],\n\u001B[32m 379\u001B[39m ).message\n",
|
| 547 |
+
"\u001B[36mFile \u001B[39m\u001B[32m~\\PycharmProjects\\Final_Assignment\\.venv\\Lib\\site-packages\\langchain_core\\language_models\\chat_models.py:355\u001B[39m, in \u001B[36mBaseChatModel._convert_input\u001B[39m\u001B[34m(self, input)\u001B[39m\n\u001B[32m 350\u001B[39m \u001B[38;5;28;01mreturn\u001B[39;00m ChatPromptValue(messages=convert_to_messages(\u001B[38;5;28minput\u001B[39m))\n\u001B[32m 351\u001B[39m msg = (\n\u001B[32m 352\u001B[39m \u001B[33mf\u001B[39m\u001B[33m\"\u001B[39m\u001B[33mInvalid input type \u001B[39m\u001B[38;5;132;01m{\u001B[39;00m\u001B[38;5;28mtype\u001B[39m(\u001B[38;5;28minput\u001B[39m)\u001B[38;5;132;01m}\u001B[39;00m\u001B[33m. \u001B[39m\u001B[33m\"\u001B[39m\n\u001B[32m 353\u001B[39m \u001B[33m\"\u001B[39m\u001B[33mMust be a PromptValue, str, or list of BaseMessages.\u001B[39m\u001B[33m\"\u001B[39m\n\u001B[32m 354\u001B[39m )\n\u001B[32m--> \u001B[39m\u001B[32m355\u001B[39m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(msg)\n",
|
| 548 |
+
"\u001B[31mValueError\u001B[39m: Invalid input type <class 'dict'>. Must be a PromptValue, str, or list of BaseMessages."
|
| 549 |
+
]
|
| 550 |
+
}
|
| 551 |
+
],
|
| 552 |
+
"execution_count": 9
|
| 553 |
+
},
|
| 554 |
+
{
|
| 555 |
+
"metadata": {
|
| 556 |
+
"ExecuteTime": {
|
| 557 |
+
"end_time": "2025-04-28T11:08:10.375646Z",
|
| 558 |
+
"start_time": "2025-04-28T11:07:56.818924Z"
|
| 559 |
+
}
|
| 560 |
+
},
|
| 561 |
+
"cell_type": "code",
|
| 562 |
+
"source": [
|
| 563 |
+
"import base64\n",
|
| 564 |
+
"import io\n",
|
| 565 |
+
"from PIL import Image\n",
|
| 566 |
+
"\n",
|
| 567 |
+
"url = \"https://agents-course-unit4-scoring.hf.space\"\n",
|
| 568 |
+
"task_id = \"cca530fc-4052-43b2-b130-b30968d8aa44\"\n",
|
| 569 |
+
"\n",
|
| 570 |
+
"file = requests.get(f\"{url}/files/{task_id}\").content\n",
|
| 571 |
+
"\n",
|
| 572 |
+
"# Load the image into a format that Pillow can handle\n",
|
| 573 |
+
"#png = Image.open(io.BytesIO(file))\n",
|
| 574 |
+
"\n",
|
| 575 |
+
"\n",
|
| 576 |
+
"image_data = base64.b64encode(file).decode(\"utf-8\")\n",
|
| 577 |
+
"\n",
|
| 578 |
+
"message = {\n",
|
| 579 |
+
" \"role\": \"user\",\n",
|
| 580 |
+
" \"content\": [\n",
|
| 581 |
+
" {\n",
|
| 582 |
+
" \"type\": \"text\",\n",
|
| 583 |
+
" \"text\": \"What do you see in the image?\",\n",
|
| 584 |
+
" },\n",
|
| 585 |
+
" {\n",
|
| 586 |
+
" \"type\": \"image\",\n",
|
| 587 |
+
" \"source_type\": \"base64\",\n",
|
| 588 |
+
" \"data\": image_data,\n",
|
| 589 |
+
" \"mime_type\": \"image/png\",\n",
|
| 590 |
+
" },\n",
|
| 591 |
+
" ],\n",
|
| 592 |
+
"}\n",
|
| 593 |
+
"response = llm.invoke([message])\n",
|
| 594 |
+
"print(response.text())"
|
| 595 |
+
],
|
| 596 |
+
"id": "348a6be5265c4552",
|
| 597 |
+
"outputs": [
|
| 598 |
+
{
|
| 599 |
+
"name": "stdout",
|
| 600 |
+
"output_type": "stream",
|
| 601 |
+
"text": [
|
| 602 |
+
"The image shows a chessboard in a complex position.\n",
|
| 603 |
+
"\n",
|
| 604 |
+
"* **White pieces:** King on a1, pawns on a3, b2, c2, Queen on a5, Rook on e3, Bishops on d3 and c3, Queen on b3, pawns on a3.\n",
|
| 605 |
+
"* **Black pieces:** King on g8, Rook on d8, Knight on d4, Bishop on e6, pawns on a6, b7, c7, g7, h7.\n",
|
| 606 |
+
"\n",
|
| 607 |
+
"It appears to be mid-game or late mid-game. White has a strong presence in the center of the board, while Black's pieces are somewhat restricted.\n"
|
| 608 |
+
]
|
| 609 |
+
}
|
| 610 |
+
],
|
| 611 |
+
"execution_count": 35
|
| 612 |
+
},
|
| 613 |
+
{
|
| 614 |
+
"metadata": {
|
| 615 |
+
"ExecuteTime": {
|
| 616 |
+
"end_time": "2025-04-28T10:11:38.429058Z",
|
| 617 |
+
"start_time": "2025-04-28T10:11:38.421435Z"
|
| 618 |
+
}
|
| 619 |
+
},
|
| 620 |
+
"cell_type": "code",
|
| 621 |
+
"source": "type(file)",
|
| 622 |
+
"id": "6d31a64d00bde967",
|
| 623 |
+
"outputs": [
|
| 624 |
+
{
|
| 625 |
+
"data": {
|
| 626 |
+
"text/plain": [
|
| 627 |
+
"bytes"
|
| 628 |
+
]
|
| 629 |
+
},
|
| 630 |
+
"execution_count": 16,
|
| 631 |
+
"metadata": {},
|
| 632 |
+
"output_type": "execute_result"
|
| 633 |
+
}
|
| 634 |
+
],
|
| 635 |
+
"execution_count": 16
|
| 636 |
+
},
|
| 637 |
+
{
|
| 638 |
+
"metadata": {
|
| 639 |
+
"ExecuteTime": {
|
| 640 |
+
"end_time": "2025-04-28T10:21:48.824755Z",
|
| 641 |
+
"start_time": "2025-04-28T10:21:48.452463Z"
|
| 642 |
+
}
|
| 643 |
+
},
|
| 644 |
+
"cell_type": "code",
|
| 645 |
+
"source": [
|
| 646 |
+
"import base64\n",
|
| 647 |
+
"import io\n",
|
| 648 |
+
"from PIL import Image\n",
|
| 649 |
+
"\n",
|
| 650 |
+
"url = \"https://agents-course-unit4-scoring.hf.space\"\n",
|
| 651 |
+
"task_id = \"cca530fc-4052-43b2-b130-b30968d8aa44\"\n",
|
| 652 |
+
"\n",
|
| 653 |
+
"file = requests.get(f\"{url}/files/{task_id}\").content\n",
|
| 654 |
+
"\n",
|
| 655 |
+
"# Load the image into a format that Pillow can handle\n",
|
| 656 |
+
"png = Image.open(io.BytesIO(file))"
|
| 657 |
+
],
|
| 658 |
+
"id": "853c311e341ef18a",
|
| 659 |
+
"outputs": [],
|
| 660 |
+
"execution_count": 20
|
| 661 |
+
},
|
| 662 |
+
{
|
| 663 |
+
"metadata": {
|
| 664 |
+
"ExecuteTime": {
|
| 665 |
+
"end_time": "2025-04-28T10:24:13.977190Z",
|
| 666 |
+
"start_time": "2025-04-28T10:24:13.968578Z"
|
| 667 |
+
}
|
| 668 |
+
},
|
| 669 |
+
"cell_type": "code",
|
| 670 |
+
"source": "type(file)",
|
| 671 |
+
"id": "aebc5d4de6ae080",
|
| 672 |
+
"outputs": [
|
| 673 |
+
{
|
| 674 |
+
"data": {
|
| 675 |
+
"text/plain": [
|
| 676 |
+
"bytes"
|
| 677 |
+
]
|
| 678 |
+
},
|
| 679 |
+
"execution_count": 24,
|
| 680 |
+
"metadata": {},
|
| 681 |
+
"output_type": "execute_result"
|
| 682 |
+
}
|
| 683 |
+
],
|
| 684 |
+
"execution_count": 24
|
| 685 |
+
},
|
| 686 |
+
{
|
| 687 |
+
"metadata": {
|
| 688 |
+
"ExecuteTime": {
|
| 689 |
+
"end_time": "2025-04-28T10:37:59.657656Z",
|
| 690 |
+
"start_time": "2025-04-28T10:37:48.309745Z"
|
| 691 |
+
}
|
| 692 |
+
},
|
| 693 |
+
"cell_type": "code",
|
| 694 |
+
"source": [
|
| 695 |
+
"url = \"https://agents-course-unit4-scoring.hf.space\"\n",
|
| 696 |
+
"task_id = \"3f57289b-8c60-48be-bd80-01f8099ca449\"\n",
|
| 697 |
+
"\n",
|
| 698 |
+
"response =requests.get(f\"{url}/files/{task_id}\")"
|
| 699 |
+
],
|
| 700 |
+
"id": "d5fe42a61ec933a7",
|
| 701 |
+
"outputs": [],
|
| 702 |
+
"execution_count": 31
|
| 703 |
+
},
|
| 704 |
+
{
|
| 705 |
+
"metadata": {
|
| 706 |
+
"ExecuteTime": {
|
| 707 |
+
"end_time": "2025-04-28T10:38:02.736542Z",
|
| 708 |
+
"start_time": "2025-04-28T10:38:02.726023Z"
|
| 709 |
+
}
|
| 710 |
+
},
|
| 711 |
+
"cell_type": "code",
|
| 712 |
+
"source": "response.headers",
|
| 713 |
+
"id": "ae31e0a04b38d72e",
|
| 714 |
+
"outputs": [
|
| 715 |
+
{
|
| 716 |
+
"data": {
|
| 717 |
+
"text/plain": [
|
| 718 |
+
"{'Date': 'Mon, 28 Apr 2025 10:38:01 GMT', 'Content-Type': 'application/json', 'Content-Length': '87', 'Connection': 'keep-alive', 'server': 'uvicorn', 'x-proxied-host': 'http://10.108.141.57', 'x-proxied-replica': 'hcbpd5es-mboj7', 'x-proxied-path': '/files/3f57289b-8c60-48be-bd80-01f8099ca449', 'link': '<https://huggingface.co/spaces/agents-course/Unit4_scoring>;rel=\"canonical\"', 'x-request-id': 'ptt6H3', 'vary': 'origin, access-control-request-method, access-control-request-headers', 'access-control-allow-credentials': 'true'}"
|
| 719 |
+
]
|
| 720 |
+
},
|
| 721 |
+
"execution_count": 32,
|
| 722 |
+
"metadata": {},
|
| 723 |
+
"output_type": "execute_result"
|
| 724 |
+
}
|
| 725 |
+
],
|
| 726 |
+
"execution_count": 32
|
| 727 |
+
},
|
| 728 |
+
{
|
| 729 |
+
"metadata": {
|
| 730 |
+
"ExecuteTime": {
|
| 731 |
+
"end_time": "2025-04-28T10:38:27.260636Z",
|
| 732 |
+
"start_time": "2025-04-28T10:38:27.251750Z"
|
| 733 |
+
}
|
| 734 |
+
},
|
| 735 |
+
"cell_type": "code",
|
| 736 |
+
"source": "response.content",
|
| 737 |
+
"id": "cae7cdd9c6a5954c",
|
| 738 |
+
"outputs": [
|
| 739 |
+
{
|
| 740 |
+
"data": {
|
| 741 |
+
"text/plain": [
|
| 742 |
+
"b'{\"detail\":\"No file path associated with task_id 3f57289b-8c60-48be-bd80-01f8099ca449.\"}'"
|
| 743 |
+
]
|
| 744 |
+
},
|
| 745 |
+
"execution_count": 33,
|
| 746 |
+
"metadata": {},
|
| 747 |
+
"output_type": "execute_result"
|
| 748 |
+
}
|
| 749 |
+
],
|
| 750 |
+
"execution_count": 33
|
| 751 |
+
},
|
| 752 |
+
{
|
| 753 |
+
"metadata": {},
|
| 754 |
+
"cell_type": "markdown",
|
| 755 |
+
"source": "# Graph Test with Image",
|
| 756 |
+
"id": "394eb37ceb5347b4"
|
| 757 |
+
},
|
| 758 |
+
{
|
| 759 |
+
"metadata": {},
|
| 760 |
+
"cell_type": "code",
|
| 761 |
+
"outputs": [],
|
| 762 |
+
"execution_count": null,
|
| 763 |
+
"source": "",
|
| 764 |
+
"id": "1686ea153ccee21"
|
| 765 |
+
},
|
| 766 |
+
{
|
| 767 |
+
"metadata": {},
|
| 768 |
+
"cell_type": "code",
|
| 769 |
+
"outputs": [],
|
| 770 |
+
"execution_count": null,
|
| 771 |
+
"source": "",
|
| 772 |
+
"id": "bd9f07c22f74d34b"
|
| 773 |
+
},
|
| 774 |
+
{
|
| 775 |
+
"metadata": {
|
| 776 |
+
"ExecuteTime": {
|
| 777 |
+
"end_time": "2025-04-28T11:24:13.797491Z",
|
| 778 |
+
"start_time": "2025-04-28T11:24:12.841550Z"
|
| 779 |
+
}
|
| 780 |
+
},
|
| 781 |
+
"cell_type": "code",
|
| 782 |
+
"source": [
|
| 783 |
+
"import requests\n",
|
| 784 |
+
"import base64\n",
|
| 785 |
+
"\n",
|
| 786 |
+
"\n",
|
| 787 |
+
"url = \"https://agents-course-unit4-scoring.hf.space\"\n",
|
| 788 |
+
"task_id = \"cca530fc-4052-43b2-b130-b30968d8aa44\"\n",
|
| 789 |
+
"\n",
|
| 790 |
+
"file = requests.get(f\"{url}/files/{task_id}\")\n",
|
| 791 |
+
"\n",
|
| 792 |
+
"content = file.content\n",
|
| 793 |
+
"\n",
|
| 794 |
+
"\n",
|
| 795 |
+
"image = base64.b64encode(content).decode(\"utf-8\")\n",
|
| 796 |
+
"\n",
|
| 797 |
+
"message = {\n",
|
| 798 |
+
" \"role\": \"user\",\n",
|
| 799 |
+
" \"content\": [\n",
|
| 800 |
+
" {\n",
|
| 801 |
+
" \"type\": \"text\",\n",
|
| 802 |
+
" \"text\": \"What do you see in the image?\",\n",
|
| 803 |
+
" },\n",
|
| 804 |
+
" {\n",
|
| 805 |
+
" \"type\": \"image\",\n",
|
| 806 |
+
" \"source_type\": \"base64\",\n",
|
| 807 |
+
" \"data\": image,\n",
|
| 808 |
+
" \"mime_type\": \"image/png\",\n",
|
| 809 |
+
" },\n",
|
| 810 |
+
" ],\n",
|
| 811 |
+
" }\n",
|
| 812 |
+
"response = graph.invoke({\n",
|
| 813 |
+
" \"messages\": [HumanMessage(content=\"What do you see in the image?\")], \"content_type\": \"image/png\", \"content\": image\n",
|
| 814 |
+
"})\n",
|
| 815 |
+
"\n",
|
| 816 |
+
"\n"
|
| 817 |
+
],
|
| 818 |
+
"id": "d6f8c031722bebde",
|
| 819 |
+
"outputs": [
|
| 820 |
+
{
|
| 821 |
+
"ename": "NameError",
|
| 822 |
+
"evalue": "name 'requests' is not defined",
|
| 823 |
+
"output_type": "error",
|
| 824 |
+
"traceback": [
|
| 825 |
+
"\u001B[31m---------------------------------------------------------------------------\u001B[39m",
|
| 826 |
+
"\u001B[31mNameError\u001B[39m Traceback (most recent call last)",
|
| 827 |
+
"\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[3]\u001B[39m\u001B[32m, line 6\u001B[39m\n\u001B[32m 3\u001B[39m url = \u001B[33m\"\u001B[39m\u001B[33mhttps://agents-course-unit4-scoring.hf.space\u001B[39m\u001B[33m\"\u001B[39m\n\u001B[32m 4\u001B[39m task_id = \u001B[33m\"\u001B[39m\u001B[33mcca530fc-4052-43b2-b130-b30968d8aa44\u001B[39m\u001B[33m\"\u001B[39m\n\u001B[32m----> \u001B[39m\u001B[32m6\u001B[39m file = \u001B[43mrequests\u001B[49m.get(\u001B[33mf\u001B[39m\u001B[33m\"\u001B[39m\u001B[38;5;132;01m{\u001B[39;00murl\u001B[38;5;132;01m}\u001B[39;00m\u001B[33m/files/\u001B[39m\u001B[38;5;132;01m{\u001B[39;00mtask_id\u001B[38;5;132;01m}\u001B[39;00m\u001B[33m\"\u001B[39m)\n\u001B[32m 8\u001B[39m content = file.content\n\u001B[32m 11\u001B[39m image = base64.b64encode(content).decode(\u001B[33m\"\u001B[39m\u001B[33mutf-8\u001B[39m\u001B[33m\"\u001B[39m)\n",
|
| 828 |
+
"\u001B[31mNameError\u001B[39m: name 'requests' is not defined"
|
| 829 |
+
]
|
| 830 |
+
}
|
| 831 |
+
],
|
| 832 |
+
"execution_count": 3
|
| 833 |
+
},
|
| 834 |
+
{
|
| 835 |
+
"metadata": {
|
| 836 |
+
"ExecuteTime": {
|
| 837 |
+
"end_time": "2025-04-28T11:27:18.431653Z",
|
| 838 |
+
"start_time": "2025-04-28T11:27:07.008526Z"
|
| 839 |
+
}
|
| 840 |
+
},
|
| 841 |
+
"cell_type": "code",
|
| 842 |
+
"source": [
|
| 843 |
+
"import requests\n",
|
| 844 |
+
"import base64\n",
|
| 845 |
+
"\n",
|
| 846 |
+
"url = \"https://agents-course-unit4-scoring.hf.space\"\n",
|
| 847 |
+
"task_id = \"cca530fc-4052-43b2-b130-b30968d8aa44\"\n",
|
| 848 |
+
"\n",
|
| 849 |
+
"file = requests.get(f\"{url}/files/{task_id}\")\n",
|
| 850 |
+
"\n",
|
| 851 |
+
"content = file.content\n",
|
| 852 |
+
"\n",
|
| 853 |
+
"\n",
|
| 854 |
+
"image = base64.b64encode(content).decode(\"utf-8\")"
|
| 855 |
+
],
|
| 856 |
+
"id": "8dfdded8b08191f9",
|
| 857 |
+
"outputs": [],
|
| 858 |
+
"execution_count": 5
|
| 859 |
+
},
|
| 860 |
+
{
|
| 861 |
+
"metadata": {
|
| 862 |
+
"ExecuteTime": {
|
| 863 |
+
"end_time": "2025-04-28T11:27:18.458424Z",
|
| 864 |
+
"start_time": "2025-04-28T11:27:18.446544Z"
|
| 865 |
+
}
|
| 866 |
+
},
|
| 867 |
+
"cell_type": "code",
|
| 868 |
+
"source": "type(image)",
|
| 869 |
+
"id": "aff8123098008c84",
|
| 870 |
+
"outputs": [
|
| 871 |
+
{
|
| 872 |
+
"data": {
|
| 873 |
+
"text/plain": [
|
| 874 |
+
"str"
|
| 875 |
+
]
|
| 876 |
+
},
|
| 877 |
+
"execution_count": 6,
|
| 878 |
+
"metadata": {},
|
| 879 |
+
"output_type": "execute_result"
|
| 880 |
+
}
|
| 881 |
+
],
|
| 882 |
+
"execution_count": 6
|
| 883 |
+
},
|
| 884 |
+
{
|
| 885 |
+
"metadata": {
|
| 886 |
+
"ExecuteTime": {
|
| 887 |
+
"end_time": "2025-04-28T14:56:48.036657Z",
|
| 888 |
+
"start_time": "2025-04-28T14:56:41.876114Z"
|
| 889 |
+
}
|
| 890 |
+
},
|
| 891 |
+
"cell_type": "code",
|
| 892 |
+
"source": [
|
| 893 |
+
"from dotenv import load_dotenv\n",
|
| 894 |
+
"from gradio.server_messages import BaseMessage\n",
|
| 895 |
+
"\n",
|
| 896 |
+
"from agent import *\n",
|
| 897 |
+
"\n",
|
| 898 |
+
"load_dotenv()\n",
|
| 899 |
+
"\n",
|
| 900 |
+
"\n",
|
| 901 |
+
"llm = get_llm()\n",
|
| 902 |
+
"\n",
|
| 903 |
+
"graph = get_graph(llm)\n"
|
| 904 |
+
],
|
| 905 |
+
"id": "9cfbff34b43717aa",
|
| 906 |
+
"outputs": [],
|
| 907 |
+
"execution_count": 1
|
| 908 |
+
},
|
| 909 |
+
{
|
| 910 |
+
"metadata": {
|
| 911 |
+
"ExecuteTime": {
|
| 912 |
+
"end_time": "2025-04-28T14:57:56.787741Z",
|
| 913 |
+
"start_time": "2025-04-28T14:57:48.550907Z"
|
| 914 |
+
}
|
| 915 |
+
},
|
| 916 |
+
"cell_type": "code",
|
| 917 |
+
"source": [
|
| 918 |
+
"import requests\n",
|
| 919 |
+
"import base64\n",
|
| 920 |
+
"\n",
|
| 921 |
+
"\n",
|
| 922 |
+
"url = \"https://agents-course-unit4-scoring.hf.space\"\n",
|
| 923 |
+
"task_id = \"cca530fc-4052-43b2-b130-b30968d8aa44\"\n",
|
| 924 |
+
"\n",
|
| 925 |
+
"file = requests.get(f\"{url}/files/{task_id}\")\n",
|
| 926 |
+
"\n",
|
| 927 |
+
"content = file.content\n",
|
| 928 |
+
"\n",
|
| 929 |
+
"\n",
|
| 930 |
+
"image = base64.b64encode(content).decode(\"utf-8\")\n",
|
| 931 |
+
"\n",
|
| 932 |
+
"response = graph.invoke({\n",
|
| 933 |
+
" \"messages\": HumanMessage(content=[\n",
|
| 934 |
+
" {\"type\": \"text\", \"text\": \"Review the chess position provided in the image. It is black's turn. Provide the correct next move for black which guarantees a win. Please provide your response in algebraic notation.\"},\n",
|
| 935 |
+
" {\n",
|
| 936 |
+
" \"type\": \"image\",\n",
|
| 937 |
+
" \"source_type\": \"base64\",\n",
|
| 938 |
+
" \"data\": image,\n",
|
| 939 |
+
" \"mime_type\": \"image/png\",\n",
|
| 940 |
+
" }\n",
|
| 941 |
+
" ]), \"content_type\": \"image/png\", \"content\": image\n",
|
| 942 |
+
"})\n",
|
| 943 |
+
"\n",
|
| 944 |
+
"\n"
|
| 945 |
+
],
|
| 946 |
+
"id": "26bf0c6bb52a851",
|
| 947 |
+
"outputs": [
|
| 948 |
+
{
|
| 949 |
+
"name": "stdout",
|
| 950 |
+
"output_type": "stream",
|
| 951 |
+
"text": [
|
| 952 |
+
"\n",
|
| 953 |
+
"-------------------- Starting to create a plan --------------------\n",
|
| 954 |
+
"\n",
|
| 955 |
+
"Content is: image/png\n",
|
| 956 |
+
"The plan is: 1. Analyze the board state.\n",
|
| 957 |
+
"2. Identify tactical motifs.\n",
|
| 958 |
+
"3. Consider forcing moves.\n",
|
| 959 |
+
"4. Evaluate consequences of each move.\n",
|
| 960 |
+
"5. Choose the move that leads to a forced win.\n",
|
| 961 |
+
"\n",
|
| 962 |
+
"-------------------- Agent has been called -----------------------------------\n",
|
| 963 |
+
"\n",
|
| 964 |
+
"Agent has made a decision:\n",
|
| 965 |
+
" Qxg3+ []\n",
|
| 966 |
+
"Waiting for 4 seconds...\n",
|
| 967 |
+
"\n",
|
| 968 |
+
"-------------------- Decision of forwarding has been made --------------------\n",
|
| 969 |
+
"\n",
|
| 970 |
+
"This is round: 2\n",
|
| 971 |
+
"The last message is: content='Qxg3+' additional_kwargs={} response_metadata={'prompt_feedback': {'block_reason': 0, 'safety_ratings': []}, 'finish_reason': 'STOP', 'model_name': 'gemini-2.0-flash', 'safety_ratings': []} id='run-a30f4635-7888-44fa-bdf8-140d2fc7da69-0' usage_metadata={'input_tokens': 1629, 'output_tokens': 5, 'total_tokens': 1634, 'input_token_details': {'cache_read': 0}}\n",
|
| 972 |
+
"The final answer is: Qxg3+\n"
|
| 973 |
+
]
|
| 974 |
+
}
|
| 975 |
+
],
|
| 976 |
+
"execution_count": 3
|
| 977 |
+
},
|
| 978 |
+
{
|
| 979 |
+
"metadata": {
|
| 980 |
+
"ExecuteTime": {
|
| 981 |
+
"end_time": "2025-04-28T11:53:52.514018Z",
|
| 982 |
+
"start_time": "2025-04-28T11:53:50.971863Z"
|
| 983 |
+
}
|
| 984 |
+
},
|
| 985 |
+
"cell_type": "code",
|
| 986 |
+
"source": [
|
| 987 |
+
"\n",
|
| 988 |
+
"msg = {\n",
|
| 989 |
+
" \"role\": \"user\",\n",
|
| 990 |
+
" \"content\": [{\n",
|
| 991 |
+
" \"type\": \"text\",\n",
|
| 992 |
+
" \"text\": \"What do you see in the image?\",\n",
|
| 993 |
+
" },\n",
|
| 994 |
+
" {\n",
|
| 995 |
+
" \"type\": \"image\",\n",
|
| 996 |
+
" \"source_type\": \"base64\",\n",
|
| 997 |
+
" \"data\": image,\n",
|
| 998 |
+
" \"mime_type\": \"image/png\",\n",
|
| 999 |
+
"\n",
|
| 1000 |
+
" }]\n",
|
| 1001 |
+
" }\n",
|
| 1002 |
+
"description = llm.invoke([msg]).content\n"
|
| 1003 |
+
],
|
| 1004 |
+
"id": "a50a4f682c5efba3",
|
| 1005 |
+
"outputs": [],
|
| 1006 |
+
"execution_count": 4
|
| 1007 |
+
},
|
| 1008 |
+
{
|
| 1009 |
+
"metadata": {
|
| 1010 |
+
"ExecuteTime": {
|
| 1011 |
+
"end_time": "2025-04-28T11:53:55.296323Z",
|
| 1012 |
+
"start_time": "2025-04-28T11:53:55.285807Z"
|
| 1013 |
+
}
|
| 1014 |
+
},
|
| 1015 |
+
"cell_type": "code",
|
| 1016 |
+
"source": "description",
|
| 1017 |
+
"id": "cba853842235c686",
|
| 1018 |
+
"outputs": [
|
| 1019 |
+
{
|
| 1020 |
+
"data": {
|
| 1021 |
+
"text/plain": [
|
| 1022 |
+
"'The image shows a chessboard in a middle-game position. White has a king on a1, pawns on a3, b2, and c2, a queen on a5, a rook on e3, bishops on e3 and c3, a queen on b3, and a pawn on a3. Black has a king on g8, pawns on b7, c7, g7, and h7, a bishop on e6, a knight on e4, and a rook on d8.'"
|
| 1023 |
+
]
|
| 1024 |
+
},
|
| 1025 |
+
"execution_count": 5,
|
| 1026 |
+
"metadata": {},
|
| 1027 |
+
"output_type": "execute_result"
|
| 1028 |
+
}
|
| 1029 |
+
],
|
| 1030 |
+
"execution_count": 5
|
| 1031 |
+
},
|
| 1032 |
{
|
| 1033 |
"metadata": {},
|
| 1034 |
"cell_type": "code",
|
| 1035 |
"outputs": [],
|
| 1036 |
"execution_count": null,
|
| 1037 |
"source": "",
|
| 1038 |
+
"id": "741f44bcb86cd88e"
|
| 1039 |
}
|
| 1040 |
],
|
| 1041 |
"metadata": {
|
playground_api.ipynb
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
CHANGED
|
@@ -11,4 +11,6 @@ langchain-groq~=0.3.2
|
|
| 11 |
langchain-community ~=0.3.22
|
| 12 |
wikipedia ~= 1.4.0
|
| 13 |
langchain-google-genai ~=2.1.3
|
| 14 |
-
tavily-python ~=0.7.0
|
|
|
|
|
|
|
|
|
| 11 |
langchain-community ~=0.3.22
|
| 12 |
wikipedia ~= 1.4.0
|
| 13 |
langchain-google-genai ~=2.1.3
|
| 14 |
+
tavily-python ~=0.7.0
|
| 15 |
+
pandas ~=2.2.3
|
| 16 |
+
matplotlib ~=3.10.1
|