Alexandre Gazola commited on
Commit
1c14abd
·
1 Parent(s): 13c4729

trocando implementacao para obtencao do FEN

Browse files
Files changed (2) hide show
  1. chess_image_to_fen_tool.py +50 -0
  2. langchain_agent.py +5 -3
chess_image_to_fen_tool.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.tools import tool
2
+ from image_to_text_tool import image_to_text
3
+ from utils import get_base64
4
+ from typing import Literal
5
+ from utils import get_base64
6
+
7
+ @tool
8
+ def chess_image_to_fen(image_path_in_base64:str, current_player: Literal["black", "white"]) -> Dict[str,str]:
9
+ """
10
+ Convert chess image to FEN (Forsyth-Edwards Notation) notation.
11
+ Args:
12
+ image_path_in_base64: Path to the image file in base64 format.
13
+ current_player: Whose turn it is to play. Must be either 'black' or 'white'.
14
+ Returns:
15
+ JSON with FEN (Forsyth-Edwards Notation) string representing the current board position.
16
+ """
17
+ print(f"Image to Fen invocada com os seguintes parametros:")
18
+ print(f"image_path: {image_path}")
19
+ print(f"current_player: {current_player}")
20
+
21
+
22
+ if current_player not in ["black", "white"]:
23
+ raise ValueError("current_player must be 'black' or 'white'")
24
+
25
+ base64_image = get_base64(chessboard_image_base64_path)
26
+
27
+ if not base64_image:
28
+ raise ValueError("Failed to encode image to base64.")
29
+ base64_image_encoded = f"data:image/jpeg;base64,{base64_image}"
30
+ url = CHESSVISION_TO_FEN_URL
31
+ payload = {
32
+ "board_orientation": "predict",
33
+ "cropped": False,
34
+ "current_player": "black",
35
+ "image": base64_image_encoded,
36
+ "predict_turn": False
37
+ }
38
+
39
+ response = requests.post(url, json=payload)
40
+ if response.status_code == 200:
41
+ dados = response.json()
42
+ if dados.get("success"):
43
+ print(f"Retorno Chessvision {dados}")
44
+ fen = dados.get("result")
45
+ fen = fen.replace("_", " ") #retorna _ no lugar de espaço em branco
46
+ return json.dumps({"fen": fen})
47
+ else:
48
+ raise Exception("Requisição feita, mas falhou na predição.")
49
+ else:
50
+ raise Exception(f"Erro na requisição: {response.status_code}")
langchain_agent.py CHANGED
@@ -15,6 +15,7 @@ from botanical_classification_tool import get_botanical_classification
15
  from excel_parser_tool import parse_excel
16
  from analyse_chess_position_tool import get_chess_best_move
17
  from convert_chessboard_image_to_fen_tool import convert_chessboard_image_to_fen
 
18
 
19
 
20
  class LangChainAgent:
@@ -22,7 +23,7 @@ class LangChainAgent:
22
  llm = ChatGoogleGenerativeAI(
23
  model=constants.MODEL,
24
  api_key=constants.API_KEY,
25
- temperature=0.0,
26
  timeout=20)
27
 
28
  tools = [
@@ -32,7 +33,8 @@ class LangChainAgent:
32
  internet_search,
33
  get_botanical_classification,
34
  parse_excel,
35
- convert_chessboard_image_to_fen,
 
36
  get_chess_best_move
37
  ]
38
 
@@ -43,7 +45,7 @@ class LangChainAgent:
43
  MessagesPlaceholder(variable_name="agent_scratchpad"),
44
  ])
45
 
46
- agent = create_openai_functions_agent(llm, tools, prompt=prompt)
47
  self.executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
48
 
49
  def __call__(self, question: str) -> str:
 
15
  from excel_parser_tool import parse_excel
16
  from analyse_chess_position_tool import get_chess_best_move
17
  from convert_chessboard_image_to_fen_tool import convert_chessboard_image_to_fen
18
+ from chess_image_to_fen_tool import chess_image_to_fen
19
 
20
 
21
  class LangChainAgent:
 
23
  llm = ChatGoogleGenerativeAI(
24
  model=constants.MODEL,
25
  api_key=constants.API_KEY,
26
+ temperature=0.4,
27
  timeout=20)
28
 
29
  tools = [
 
33
  internet_search,
34
  get_botanical_classification,
35
  parse_excel,
36
+ #convert_chessboard_image_to_fen,
37
+ chess_image_to_fen,
38
  get_chess_best_move
39
  ]
40
 
 
45
  MessagesPlaceholder(variable_name="agent_scratchpad"),
46
  ])
47
 
48
+ agent = create_tool_calling_agent(llm, tools, prompt=prompt)
49
  self.executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
50
 
51
  def __call__(self, question: str) -> str: