22Nikk0 commited on
Commit
027d411
·
verified ·
1 Parent(s): a8cb063

Update agentic.py

Browse files
Files changed (1) hide show
  1. agentic.py +68 -46
agentic.py CHANGED
@@ -51,7 +51,7 @@ class State(TypedDict):
51
  video_node_result: AnyMessage
52
  audio_node_result: AnyMessage
53
  code_node_result: AnyMessage
54
- next: str
55
 
56
  ########################
57
 
@@ -306,7 +306,7 @@ You do not handle code
306
  3. Report your thought process in detail, explaining your reasoning step-by-step.
307
 
308
  Here is the question {state['question']}
309
- Now provide your response immediately without any preamble.
310
  """
311
 
312
  state["thinking_node_result"] = state.get("thinking_node_result", "")
@@ -352,7 +352,7 @@ You do not handle audio
352
  Here is the question : {state['question']}
353
  Here is the code : {code}
354
 
355
- Now provide your response immediately without any preamble.
356
  """
357
 
358
  sys_msg = SystemMessage(content=prompt)
@@ -410,7 +410,7 @@ wikipedia_search:
410
  {wikipedia_search.response_format}
411
 
412
  Here is the question {state['question']}
413
- Now provide your response immediately without any preamble.
414
  """
415
 
416
  state["web_wiki_search_node_result"] = state.get("web_wiki_search_node_result", "")
@@ -451,7 +451,7 @@ You do not handle code
451
  4. Report your thought process in detail, explaining your reasoning step-by-step.
452
 
453
  Here is the question {state['question']}
454
- Now provide your response immediately without any preamble.
455
  """
456
 
457
  image_base64 = ""
@@ -540,7 +540,7 @@ You do not handle code.
540
  6. Do not make up information that is not visible or inferable from the video.
541
 
542
  Here is the question {state['question']}
543
- Now provide your response immediately without any preamble.
544
  """
545
 
546
  if re.search(r'youtube\.com', state["question"]):
@@ -623,7 +623,7 @@ You do not handle code.
623
  6. Do not make up information that is not audible or inferable from the audio.
624
 
625
  Here is the question {state['question']}
626
- Now provide your response immediately without any preamble.
627
  """
628
 
629
  downloaded_audio = state["downloaded_file"]
@@ -691,16 +691,18 @@ FINAL ANSWER: [YOUR FINAL ANSWER]
691
  - Write the string without articles (a, an, the).
692
  - Don't answer a full sentence when a short version is enough.
693
  - Do not use abbreviations (e.g., for cities).
694
- - Write digits in plain text (e.g., "one" instead of "1") unless specified otherwise.
 
695
  - If asked for a comma-separated list:
696
  - Apply the above rules for numbers and strings to each element in the list.
 
697
 
698
  ## Constraints
699
  - You must not answer if the constraints above are not respected.
700
  - Your final answer should be provided in the format: FINAL ANSWER: [YOUR FINAL ANSWER]
701
  - Your final answer should be a number, a string, or a comma-separated list of numbers and/or strings, following the specified formatting rules.
702
 
703
- Now provide your response immediately without any preamble.
704
  """
705
 
706
  nodes_response = [HumanMessage(content="Here are the results of the previous nodes")]
@@ -760,11 +762,11 @@ Here are the nodes you can choose:
760
  Here is the question : {state['question']}
761
  Here is the file : {state.get("input_file", "no file to handle")}
762
 
763
- Now provide your response immediately respecting this format: 'next node: \'the node name you choose\''.
764
  """
765
 
766
  downloaded = ""
767
- # If there's an input file, download it directlynext node:
768
  if state.get("input_file", None):
769
  downloaded = download_input_file(state.get("task_id"))
770
 
@@ -774,15 +776,18 @@ Now provide your response immediately respecting this format: 'next node: \'the
774
 
775
  entry_node_response[-1].pretty_print()
776
 
777
- regex_result = re.search(r'.*next\snode:\s+(?P<next_node>[a-z_]*)$', entry_node_response[-1].content)
778
 
779
  next_node = "END"
780
  if regex_result:
781
  # Extract the node name and remove any quotes around it
782
- next_node = regex_result.group("next_node").strip().strip('\'"')
 
 
 
783
 
784
  return {
785
- "next": next_node,
786
  "downloaded_file": downloaded
787
  }
788
 
@@ -806,7 +811,7 @@ def build_graph():
806
  # Conditional routing from entry_node to specialized nodes
807
  builder.add_conditional_edges(
808
  "entry_node",
809
- lambda state: state["next"],
810
  {
811
  "web_wiki_search_node": "web_wiki_search_node",
812
  "vision_node": "vision_node",
@@ -839,40 +844,57 @@ if __name__ == "__main__":
839
 
840
  # print(vision_node.__doc__)
841
 
842
- input = {
843
- "task_id": "cca530fc-4052-43b2-b130-b30968d8aa44",
844
- "question": "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.",
845
- "Level": "1",
846
- "file_name": "cca530fc-4052-43b2-b130-b30968d8aa44.png"
847
- }
848
 
849
- question = input.get("question", "No question found")
850
- file_name = input.get("file_name", "")
851
- task_id = input.get("task_id", "")
 
 
 
852
 
853
- print(f"QUESTION : {question}")
854
- print(f"FILE: {file_name}")
855
 
856
- user_prompt = [HumanMessage(content=f"Can you answer the question please ?")]
857
 
858
- user_input = {"messages": user_prompt, "question": question, "input_file": file_name, "task_id": task_id}
 
 
859
 
860
- messages = agent_graph.invoke(
861
- input=user_input,
862
- config={
863
- "recursion_limit": 10,
864
- # "callbacks": [langfuse_handler]
865
- }
866
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
867
 
868
- for m in messages['messages']:
869
- m.pretty_print()
870
-
871
- try:
872
- regex_result = re.search(r"FINAL ANSWER:\s*(?P<answer>.*)$", messages['messages'][-1].content)
873
- answer = regex_result.group("answer")
874
- except:
875
- regex_result = re.search(r"\s*(?P<answer>.*)$", messages['messages'][-1].content)
876
- answer = regex_result.group("answer")
877
-
878
- print(answer)
 
51
  video_node_result: AnyMessage
52
  audio_node_result: AnyMessage
53
  code_node_result: AnyMessage
54
+ next_node: str
55
 
56
  ########################
57
 
 
306
  3. Report your thought process in detail, explaining your reasoning step-by-step.
307
 
308
  Here is the question {state['question']}
309
+ Now provide your response immediately without any preamble in text but not in markdown.
310
  """
311
 
312
  state["thinking_node_result"] = state.get("thinking_node_result", "")
 
352
  Here is the question : {state['question']}
353
  Here is the code : {code}
354
 
355
+ Now provide your response immediately without any preamble in text but not in markdown.
356
  """
357
 
358
  sys_msg = SystemMessage(content=prompt)
 
410
  {wikipedia_search.response_format}
411
 
412
  Here is the question {state['question']}
413
+ Now provide your response immediately without any preamble in text but not in markdown.
414
  """
415
 
416
  state["web_wiki_search_node_result"] = state.get("web_wiki_search_node_result", "")
 
451
  4. Report your thought process in detail, explaining your reasoning step-by-step.
452
 
453
  Here is the question {state['question']}
454
+ Now provide your response immediately without any preamble in text but not in markdown.
455
  """
456
 
457
  image_base64 = ""
 
540
  6. Do not make up information that is not visible or inferable from the video.
541
 
542
  Here is the question {state['question']}
543
+ Now provide your response immediately without any preamble in text but not in markdown.
544
  """
545
 
546
  if re.search(r'youtube\.com', state["question"]):
 
623
  6. Do not make up information that is not audible or inferable from the audio.
624
 
625
  Here is the question {state['question']}
626
+ Now provide your response immediately without any preamble in text but not in markdown.
627
  """
628
 
629
  downloaded_audio = state["downloaded_file"]
 
691
  - Write the string without articles (a, an, the).
692
  - Don't answer a full sentence when a short version is enough.
693
  - Do not use abbreviations (e.g., for cities).
694
+ - Write digits in text but (e.g., "one" instead of "1") unless specified otherwise.
695
+ - Start the first word with a capital letter.
696
  - If asked for a comma-separated list:
697
  - Apply the above rules for numbers and strings to each element in the list.
698
+ - And take care of having a space after each comma.
699
 
700
  ## Constraints
701
  - You must not answer if the constraints above are not respected.
702
  - Your final answer should be provided in the format: FINAL ANSWER: [YOUR FINAL ANSWER]
703
  - Your final answer should be a number, a string, or a comma-separated list of numbers and/or strings, following the specified formatting rules.
704
 
705
+ Now provide your response immediately without any preamble in text but not in markdown.
706
  """
707
 
708
  nodes_response = [HumanMessage(content="Here are the results of the previous nodes")]
 
762
  Here is the question : {state['question']}
763
  Here is the file : {state.get("input_file", "no file to handle")}
764
 
765
+ Now provide your response immediately respecting this format in lower case: 'next node: \'the node name you choose\''.
766
  """
767
 
768
  downloaded = ""
769
+ # If there's an input file, download it directly:
770
  if state.get("input_file", None):
771
  downloaded = download_input_file(state.get("task_id"))
772
 
 
776
 
777
  entry_node_response[-1].pretty_print()
778
 
779
+ regex_result = re.search(r'.*next\s*node:\s*(?P<next_node>[a-z_]*).*$', entry_node_response[-1].content, re.IGNORECASE)
780
 
781
  next_node = "END"
782
  if regex_result:
783
  # Extract the node name and remove any quotes around it
784
+ next_node = regex_result.group("next_node")
785
+ next_node = next_node.lower()
786
+
787
+ print(f"Next node to invoke: {next_node}")
788
 
789
  return {
790
+ "next_node": next_node,
791
  "downloaded_file": downloaded
792
  }
793
 
 
811
  # Conditional routing from entry_node to specialized nodes
812
  builder.add_conditional_edges(
813
  "entry_node",
814
+ lambda state: state["next_node"],
815
  {
816
  "web_wiki_search_node": "web_wiki_search_node",
817
  "vision_node": "vision_node",
 
844
 
845
  # print(vision_node.__doc__)
846
 
847
+ with open("./responses.json", "r") as responses:
848
+ json_responses = json.loads(responses.read())
 
 
 
 
849
 
850
+ # input = {
851
+ # "task_id": "cca530fc-4052-43b2-b130-b30968d8aa44",
852
+ # "question": "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.",
853
+ # "Level": "1",
854
+ # "file_name": "cca530fc-4052-43b2-b130-b30968d8aa44.png"
855
+ # }
856
 
857
+ with open("questions.json", "r") as questions:
858
+ json_questions = json.loads(questions.read())
859
 
860
+ for input in json_questions:
861
 
862
+ question = input.get("question", "No question found")
863
+ file_name = input.get("file_name", "")
864
+ task_id = input.get("task_id", "")
865
 
866
+ print(f"QUESTION : {question}")
867
+ print(f"FILE: {file_name}")
868
+
869
+ user_prompt = [HumanMessage(content="Can you answer the question please ?")]
870
+
871
+ user_input = {"messages": user_prompt, "question": question, "input_file": file_name, "task_id": task_id}
872
+
873
+ messages = agent_graph.invoke(
874
+ input=user_input,
875
+ config={
876
+ "recursion_limit": 10,
877
+ # "callbacks": [langfuse_handler]
878
+ }
879
+ )
880
+
881
+ for m in messages['messages']:
882
+ m.pretty_print()
883
+
884
+ try:
885
+ regex_result = re.search(r"FINAL ANSWER:\s*(?P<answer>.*)$", messages['messages'][-1].content)
886
+ answer = regex_result.group("answer")
887
+ except:
888
+ regex_result = re.search(r"\s*(?P<answer>.*)$", messages['messages'][-1].content)
889
+ answer = regex_result.group("answer")
890
+
891
+ print(answer)
892
+ if answer == json_responses.get(task_id, ""):
893
+ print("The answer is correct !")
894
+ else:
895
+ print("The answer is incorrect !")
896
+ print(f"Expected: {json_responses.get(task_id, '')}")
897
+ print(f"Got: {answer}")
898
+
899
+ time.sleep(10)
900