Added system prompt

This commit is contained in:
2024-08-17 13:00:37 -05:00
parent fd549c203e
commit 255153af56
+56 -24
View File
@@ -1,3 +1,4 @@
import json
import os import os
import importlib import importlib
import inspect import inspect
@@ -16,6 +17,26 @@ client = OpenAI()
GPT_4O = "gpt-4o" GPT_4O = "gpt-4o"
GPT_4O_MINI = "gpt-4o-mini" GPT_4O_MINI = "gpt-4o-mini"
system_prompt = """Alright, imagine you're a savvy developer with a trusty toolkit. Your mission: to manage a repository like a maestro conducting an orchestra. You don't just wield tools; you dance with them. Let's craft a persona that embodies the essence of a repository wizard:
Organized Explorer: You know your way around the file system. You can list files in a directory with ease, and if a file's content is what you seek, reading it is a breeze.
Branch Botanist: Branches are your garden. You plant new ones, name them creatively, and make sure they stem from the right place. You keep an eye out for the SHA of the latest commit just for good measure.
Persistent Committer: Committing changes is your thrill. You've mastered the art of committing files with purpose, leaving behind a trail of meaningful messages.
Pull Request Protagonist: The stage is set for collaboration. You create pull requests with compelling titles and bodies, ensuring your contributions are seen and valued.
Code Detective: Whether it's tracking changes or searching for specific code, your investigative skills are top-notch. Histories and queries bend to your inquisitive will.
Guardian of the Current: You're always aware of your current branch, and can pivot as needed. Setting and getting the current branch is second nature to you.
Archaeologist of Commits: Need to dig up an old file version? No problem. You retrieve file contents from specific commit SHAs like unearthing hidden treasures.
Branch Cartographer: Charting out all branches helps you understand the lay of the land. You list them to keep track of your project's evolving terrain.
Imagine the possibilities—each tool a note in your grand symphony of repository management. No need to memorize; just embody the spirit of curiosity, precision, and orchestration. Ready to dive in?
"""
# Set up Telegram bot # Set up Telegram bot
TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN') TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
@@ -82,7 +103,7 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
conversation_history[user_id].append({"role": "user", "content": user_message}) conversation_history[user_id].append({"role": "user", "content": user_message})
# Prepare messages for OpenAI API # Prepare messages for OpenAI API
messages = [{"role": "system", "content": "You are a helpful assistant."}] + conversation_history[user_id] messages = [{"role": "system", "content": system_prompt}] + conversation_history[user_id]
# Check if there's an image to process # Check if there's an image to process
if user_id in user_images: if user_id in user_images:
@@ -103,48 +124,41 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
], ],
} }
], ],
max_tokens=2048, max_tokens=16384
) )
# Remove the temporary image file # Remove the temporary image file
os.remove(user_images[user_id]) os.remove(user_images[user_id])
del user_images[user_id] del user_images[user_id]
else: else:
# Call OpenAI API for inference (text-only) # Call OpenAI API for inference (text-only)
response = client.chat.completions.create( response = get_chat_response(client, messages, 4096, GPT_4O)
model=GPT_4O,
messages=messages,
functions=functions,
function_call="auto"
)
# Extract the assistant's reply # Extract the assistant's reply
assistant_message = response.choices[0].message assistant_message = response.choices[0].message
toolUseCount = 0
if hasattr(assistant_message, 'function_call') and assistant_message.function_call: if hasattr(assistant_message, 'function_call') and assistant_message.function_call:
# Execute the function while hasattr(assistant_message, 'function_call') and assistant_message.function_call and toolUseCount < 10:
function_name = assistant_message.function_call.name tool_response = call_tool(assistant_message.function_call, messages)
function_args = assistant_message.function_call.arguments
for tool in tools: conversation_history[user_id].append({"role": "function", "name": assistant_message.function_call.name, "content": json.dumps(tool_response)})
if function_name in [f["name"] for f in tool.get_functions()]:
function_response = tool.execute(function_name, **eval(function_args))
messages.append({ messages.append({
"role": "function", "role": "function",
"name": function_name, "name": assistant_message.function_call.name,
"content": function_response "content": json.dumps(tool_response)
}) })
# Call API again to get the final response # Call API again to get the final response
response = client.chat.completions.create( assistant_message = get_chat_response(client, messages, 4096, GPT_4O).choices[0].message
model=GPT_4O, if not hasattr(assistant_message, 'function_call') or not assistant_message.function_call:
messages=messages assistant_reply = assistant_message.content
) conversation_history[user_id].append({"role": "assistant", "content": assistant_reply})
assistant_reply = response.choices[0].message.content
break
else: else:
assistant_reply = assistant_message.content assistant_reply = assistant_message.content
# Add assistant's reply to conversation history # Add assistant's reply to conversation history
conversation_history[user_id].append({"role": "assistant", "content": assistant_reply}) conversation_history[user_id].append({"role": "assistant", "content": assistant_reply})
# Trim conversation history if it gets too long (e.g., keep last 10 messages) # Trim conversation history if it gets too long (e.g., keep last 10 messages)
if len(conversation_history[user_id]) > 10: if len(conversation_history[user_id]) > 10:
conversation_history[user_id] = conversation_history[user_id][-10:] conversation_history[user_id] = conversation_history[user_id][-10:]
@@ -156,6 +170,24 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
print(f"An error occurred: {str(e)}") print(f"An error occurred: {str(e)}")
await update.message.reply_text("Sorry, an error occurred while processing your request.") await update.message.reply_text("Sorry, an error occurred while processing your request.")
def call_tool(function_call, messages):
# Execute the function
function_name = function_call.name
function_args = function_call.arguments
for tool in tools:
if function_name in [f["name"] for f in tool.get_functions()]:
return tool.execute(function_name, **eval(function_args))
def get_chat_response(client, messages, max_tokens, model):
response = client.chat.completions.create(
model=model,
messages=messages,
functions=functions,
function_call="auto",
max_tokens=max_tokens
)
return response
def main() -> None: def main() -> None:
# Create the Application and pass it your bot's token # Create the Application and pass it your bot's token
application = Application.builder().token(TELEGRAM_BOT_TOKEN).build() application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()