-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
179 lines (142 loc) · 5.14 KB
/
client.py
File metadata and controls
179 lines (142 loc) · 5.14 KB
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
"""
Google genai client.
"""
import os
import subprocess
from typing import Callable
from pathlib import Path
from google import genai
from google.genai import types
from pylspclient.lsp_client import LspClient
from pylspclient.lsp_endpoint import LspEndpoint
from pylspclient.json_rpc_endpoint import JsonRpcEndpoint
from pylspclient.lsp_pydantic_strcuts import TextDocumentIdentifier, Position
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
def another_function(a: float, b: int):
return (a ** 2) + (a * b)
def my_function(a: int, b: float):
if a == 0:
x = b ** 2
y = 1
else:
x = b / a
y = -1
z = another_function(x, y) # 32:9
return z
tools: dict[str, Callable] = {}
def register_tool(func):
tools[func.__name__] = func
return func
@register_tool
def get_function_definition(function_name: str):
"""
given the name of a function, returns the function definition
"""
# functions = {"another_function": another_function_text}
functions = {}
if function_name not in functions:
raise ValueError(f"{function_name} not available")
return functions[function_name]
function = types.FunctionDeclaration(
name='get_function_definition',
description='Get the definition of a function by name',
parameters=types.Schema(
type='OBJECT',
properties={
'function_name': types.Schema(
type='STRING',
description='the name of the function',
),
},
required=['function_name'],
),
)
tool = types.Tool(function_declarations=[function])
class GenAIClient:
"""
A simple client for interacting with the Google Generative AI API.
"""
def __init__(self):
"""
Initializes the GenAIClient with a specified model.
Args:
model_name (str): The name of the generative model to use.
Defaults to "gemini-1.0-pro".
"""
self.client = genai.Client(api_key=GEMINI_API_KEY)
self.model_name = "gemini-2.0-flash"
# self.model_name = "gemini-2.5-pro-preview-05-06"
def generate_text(self, prompt):
"""
Generates text based on the given prompt.
Args:
prompt (str): The input prompt for text generation.
Returns:
str: The generated text, or None if an error occurred.
"""
config=types.GenerateContentConfig(
tools=[tool],
automatic_function_calling=types.AutomaticFunctionCallingConfig(
maximum_remote_calls=2,
),
tool_config=types.ToolConfig(
function_calling_config=types.FunctionCallingConfig(mode='ANY', allowed_function_names=["get_function_definition"]),
),
)
max_iterations = 2
content = [prompt]
try:
response = self.client.models.generate_content(
model=self.model_name,
# system_instruction=["you are an expert coding assistant that thinks deeply about fixing and writing code"],
contents=content,
config=config,
)
tool_results = []
for _ in range(max_iterations):
if response.function_calls is None:
break
for function_call in response.function_calls:
if function_call.name not in tools:
raise KeyError(f"{function_call.name} not registered as a tool")
tool_result = tools[function_call.name](**function_call.args)
tool_results.append(tool_result)
return response.text
except Exception as e:
print(f"Error generating text: {e}")
return None
def test_mcp():
# Example usage:
client = GenAIClient()
prompt = f"""
If I pass a = 1 and b = 10 to:
my_function()
what is the result?
"""
generated_text = client.generate_text(prompt)
if generated_text:
print("Generated Text:")
print(generated_text)
class Pylsp:
def __enter__(self):
self.proc = subprocess.Popen("uv run pylsp -vvv --log-file test_pylsp.log".split(" "), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return self.proc
def __exit__(self, exc_type, exc_value, traceback):
self.proc.kill()
self.proc.communicate()
def test_lsp():
with Pylsp() as pylsp:
assert pylsp.stdin is not None and pylsp.stdout is not None
endpoint = JsonRpcEndpoint(pylsp.stdin, pylsp.stdout)
root_uri = Path(__file__).parents[2].as_uri()
lsp_endpoint = LspEndpoint(endpoint)
lsp_client = LspClient(lsp_endpoint=lsp_endpoint)
lsp_client.initialize(rootUri=root_uri, capabilities={})
uri = Path(__file__).as_uri().__str__()
document = TextDocumentIdentifier(uri=uri)
# lsp counts lines/characters from 0, vim counts from 1
position = Position(line=31, character=9)
res = lsp_client.definition(document, position)
return res
if __name__ == "__main__":
test_lsp()