Lumintroll commited on
Commit
0a55bd6
·
1 Parent(s): 2b2210b

Added tools. It's a me!

Browse files
Files changed (3) hide show
  1. agent_tools.py +1 -1
  2. scratchpad.py +133 -0
  3. test.py +22 -0
agent_tools.py CHANGED
@@ -12,6 +12,7 @@ from youtube_transcript_api import YouTubeTranscriptApi
12
  from langchain.docstore.document import Document
13
  from langchain.text_splitter import RecursiveCharacterTextSplitter
14
  from langchain_community.retrievers import BM25Retriever
 
15
 
16
  #YouTube Transcriber
17
  @tool
@@ -143,7 +144,6 @@ def text_from_pdf(pdf_file: str) -> str:
143
  content = ' '.join([page.extract_text() for page in pdf.pages])
144
  content = content.replace('\n', ' ')
145
 
146
-
147
  #vector store
148
 
149
  #string reverse
 
12
  from langchain.docstore.document import Document
13
  from langchain.text_splitter import RecursiveCharacterTextSplitter
14
  from langchain_community.retrievers import BM25Retriever
15
+ import pdfplumber
16
 
17
  #YouTube Transcriber
18
  @tool
 
144
  content = ' '.join([page.extract_text() for page in pdf.pages])
145
  content = content.replace('\n', ' ')
146
 
 
147
  #vector store
148
 
149
  #string reverse
scratchpad.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+
3
+ load_dotenv()
4
+
5
+ use_mistral = False
6
+ use_gemini = True
7
+
8
+ import base64
9
+ import requests
10
+ import os
11
+ from mistralai import Mistral
12
+
13
+ def encode_image(image_path):
14
+ """Encode the image to base64."""
15
+ try:
16
+ with open(image_path, "rb") as image_file:
17
+ return base64.b64encode(image_file.read()).decode('utf-8')
18
+ except FileNotFoundError:
19
+ print(f"Error: The file {image_path} was not found.")
20
+ return None
21
+ except Exception as e: # Added general exception handling
22
+ print(f"Error: {e}")
23
+ return None
24
+
25
+ # Path to your image
26
+ image_path = "chess_test.jpg"
27
+
28
+ # Getting the base64 string
29
+ base64_image = encode_image(image_path)
30
+
31
+ # Retrieve the API key from environment variables
32
+ api_key = os.environ.get("API_KEY_MISTRAL")
33
+
34
+ # Specify model
35
+ model = "pixtral-large-latest"
36
+
37
+ # Initialize the Mistral client
38
+ client = Mistral(api_key=api_key)
39
+
40
+ # Define the messages for the chat
41
+ messages = [
42
+ {
43
+ "role": "user",
44
+ "content": [
45
+ {
46
+ "type": "text",
47
+ "text": r"""Below is an image of a chess board mid-game. Only use the image as a reference for the response. NEVER use implicit knowledge of chess or positions.
48
+ The bottom left square is A1, the top right square is H8.
49
+
50
+ Identify the position of all pieces in JSON format: {colour:{piece_type:[coordinates]}}
51
+
52
+ Chess board diagram:"""
53
+ },
54
+ {
55
+ "type": "image_url",
56
+ "image_url": f"data:image/jpeg;base64,{base64_image}"
57
+ }
58
+ ]
59
+ }
60
+ ]
61
+
62
+ if use_mistral:
63
+ # Get the chat response
64
+ chat_response = client.chat.complete(
65
+ model=model,
66
+ messages=messages
67
+ )
68
+
69
+ # Print the content of the response
70
+ print(chat_response.choices[0].message.content)
71
+
72
+ #### Gemini
73
+
74
+ from google import genai
75
+ from google.genai import types
76
+
77
+ # Only run this block for Gemini Developer API
78
+
79
+ client = genai.Client(api_key=os.environ.get("API_KEY_GEMINI2"))
80
+ flash = True
81
+
82
+ if flash:
83
+ google_model = 'gemini-2.5-flash-preview-05-20'
84
+ else:
85
+ google_model = 'gemini-2.5-pro-preview-05-06'
86
+
87
+ chess_prompt = """Using this image of a chess board diagram. Black squares are coloured dark brown, white squares are light brown. A1 is at the bottom left of the image, H8 is at the top right. Complete the following tasks in order:
88
+ Task 1: Count the number of occupied and unoccupied squares in each row. e.g. {'occupied':3, 'unoccupied':5} => STRING
89
+ Task 2: Count the number of each piece type in each row. Check that they add up to the total number of pieces. => STRING
90
+ Task 3: In JSON format note the position of every piece by colour, type and then list of coordinates => JSON
91
+ Task 4: Convert JSON format to FEN string. {'board_fen': <FEN STRING>} => JSON"""
92
+
93
+
94
+ # To run this code you need to install the following dependencies:
95
+ # pip install google-genai
96
+
97
+ import base64
98
+ import os
99
+ from google import genai
100
+ from google.genai import types
101
+
102
+ def generate():
103
+ client = genai.Client(
104
+ api_key=os.environ.get("GEMINI_API_KEY"),
105
+ )
106
+
107
+ model = google_model #"gemini-2.5-pro-preview-05-06"
108
+ contents = [
109
+ types.Content(
110
+ role="user",
111
+ parts=[
112
+ types.Part.from_bytes(
113
+ mime_type="image/jpeg",
114
+ data=base64_image,
115
+ ),
116
+ types.Part.from_text(text=chess_prompt),
117
+ ],
118
+ ),
119
+ ]
120
+ generate_content_config = types.GenerateContentConfig(
121
+ temperature=0.15,
122
+ response_mime_type="text/plain",
123
+ )
124
+
125
+ for chunk in client.models.generate_content_stream(
126
+ model=model,
127
+ contents=contents,
128
+ config=generate_content_config,
129
+ ):
130
+ print(chunk.text, end="")
131
+
132
+ if __name__ == "__main__":
133
+ generate()
test.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import pandas as pd
3
+ from io import BytesIO
4
+
5
+ # URL of the Excel file
6
+ url = r'https://datamillnorth.org/download/2nx5n/a3be8bde-b1d5-4da1-bdf9-258c994c6960/Copy%20of%20Q1%2023-24%20Data%20Mill.xlsx'
7
+
8
+ # Send a GET request to the URL
9
+ response = requests.get(url)
10
+
11
+ # Check if the request was successful
12
+ if response.status_code == 200:
13
+ # Use BytesIO to read the content of the response as a binary stream
14
+ excel_file = BytesIO(response.content)
15
+
16
+ # Read the Excel file into a DataFrame
17
+ df = pd.read_excel(excel_file)
18
+
19
+ # Display the DataFrame
20
+ print(df)
21
+ else:
22
+ print(f"Failed to retrieve the file. Status code: {response.status_code}")