Compare commits

..

3 Commits

Author SHA1 Message Date
=
bc6115442d optimize code 2025-04-26 21:58:44 +08:00
=
c1029e0ab7 add write_file_batch as tool 2025-04-26 21:58:22 +08:00
=
294750d48b add ability to generate projects 2025-04-26 21:57:13 +08:00
3 changed files with 112 additions and 9 deletions

View File

@ -19,6 +19,27 @@ tools = [
},
"required": ["file_path","content"],
},
},
},
{
"type": "function",
"function": {
"name": "write_file_batch",
"description": "write contents files, creating directories if they do not exist. If the file already exists, it will be overwritten.",
"parameters": {
"type": "object",
"properties": {
"file_paths": {
"type": "array",
"description": "the path with file name to write to. it can be relative path. e.g. '[test_data/output/test.md]'",
},
"contents": {
"type": "array",
"description": "the contents to write to the files.",
},
},
"required": ["file_paths","contents"],
},
}
},
]

86
gen_project.py Normal file
View File

@ -0,0 +1,86 @@
import time
import json
from openai import OpenAI
import chat_tools
from bot_util import get_api_details
# initialize parameters
MODEL = "deepseek-chat"
chat_tools = chat_tools.get_chat_tools()
history = []
# Initialize the OpenAI client with the API key and base URL
client = OpenAI(
api_key=get_api_details().api_key,
base_url=get_api_details().api_url,
)
MAX_RETRIES = 3
RETRY_DELAY = 2 # seconds
count = 0
system_prompt = "You are a helpful assistant that good at writing programs. you are able to generate project structures and write code. you can only help user when you know enought information about the project. For example, if it is a maven project, you need to know the group id, artifact id, main class name, main method name and the dependencies. If the user does not provide enough information, you should ask the user for more details."
content = ""
with open("test_data/k8s_day10.txt", "r") as f:
for line in f:
content += line
def chat_with_deepseek(max_retries: int =MAX_RETRIES, retry_delay: int =RETRY_DELAY):
# Initialize the chat history with the system prompt
global content
history.append({"role": "system", "content": system_prompt})
history.append({"role": "user", "content": f"please consturct project with below requirements: \n \
I need a maven project with group id dev.kualu and artifact id demo. \
The dependency is spring-boot-starter-web. The main class is com.kualu.demo.DemoApplication. \
The main method should be in the DemoApplication class. Please generate the project foles along with pom.xml. \
Please write these files to local disk."})
count = 0
for _ in range(max_retries):
try:
response = client.chat.completions.create(
model=MODEL,
messages=history,
tools=chat_tools.get_tools(),
)
if response.choices[0].message.tool_calls:
tool_call = response.choices[0].message.tool_calls[0]
tool_name = tool_call.function.name
tool_args = tool_call.function.arguments
tool_id = tool_call.id
# Call the function with the arguments
if tool_name == "write_file_batch":
params = json.loads(tool_args)
file_path = params["file_paths"]
content = params["contents"]
result = chat_tools.util.write_file_batch(file_path, content)
# history.append(response.choices[0].message)
history.append({"role": "tool", "tool_call_id": tool_id, "content": result})
response = client.chat.completions.create(
model=MODEL,
messages=history,
)
return response.choices[0].message.content
else:
# for chunk in response:
# text = chunk.choices[0].delta.content
# if text:
# sys.stdout.write(text)
# sys.stdout.flush()
return response.choices[0].message.content
break
except Exception as e:
count += 1
print(f"An error occurre: {e}.")
if count < max_retries:
print(f"Retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
else:
print("Max retries reached. Exiting.")
raise e
print(chat_with_deepseek(max_retries=1))

View File

@ -1,24 +1,21 @@
from dotenv import load_dotenv
import os, time
import time
import json
from openai import OpenAI
import chat_tools
load_dotenv()
from bot_util import get_api_details
# initialize parameters
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
DEEPSEEK_API_URL = os.getenv("DEEPSEEK_API_URL")
MODEL = "deepseek-chat"
chat_tools = chat_tools.get_chat_tools()
history = []
# Initialize the OpenAI client with the API key and base URL
client = OpenAI(
api_key=DEEPSEEK_API_KEY,
base_url=DEEPSEEK_API_URL,
api_key=get_api_details().api_key,
base_url=get_api_details().api_url,
)
MAX_RETRIES = 3
RETRY_DELAY = 2 # seconds
count = 0
@ -52,7 +49,6 @@ def chat_with_deepseek(max_retries: int =MAX_RETRIES, retry_delay: int =RETRY_DE
tool_name = tool_call.function.name
tool_args = tool_call.function.arguments
tool_id = tool_call.id
# Call the function with the arguments
if tool_name == "write_file":
params = json.loads(tool_args)