Backend2 / utils /functions.py
vaibhavard
changes
d64b2b3
import re
import helpers.helper as helper
import requests
import json
import os
from function_support import _function
def extract_links(text):
# Regular expression pattern to match URLs
url_pattern = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
# Find all matches of the URL pattern in the text
urls = re.findall(url_pattern, text)
return urls
def allocate(messages,api_keys,model,functs):
helper.models=model
if "gemini" not in model:
for msg in messages:
if isinstance(msg["content"],list):
msg["content"]=msg["content"][0]["text"]
# for msgs in msg["content"]:
# if msgs["type"]=="image_url":
# if "base64," in msgs["image_url"]:
# helper.data["imageBase64"]=msgs["image_url"]
# print(helper.data["imageBase64"]+"base")
# else:
# helper.data["imageURL"]=msgs["image_url"]["url"]
# print(helper.data["imageURL"]+"a")
for msg in messages:
if "tool" in msg["role"]:
msg["role"]="user"
msg["content"]=f"Tool {msg['name']} returned response: {msg['content']}. Now you must output the next tool Call or respond to user in natural language after the task has been completed. "
del msg['name']
del msg["tool_call_id"]
if "tool_calls" in msg:
add=""
for tools in msg["tool_calls"]:
add=f"""
```json
[
{{
"tool":"{tools["function"]["name"]}",
"tool_input":{tools["function"]["arguments"]}
}}
]
```"""
msg["content"]=add
del msg["tool_calls"]
if functs !=[]:
print("ADDDDDDDDDDDDDDDDING TOOOOLS")
function_call=_function(tools=functs)
messages.insert(1,{"role": "system", "content": function_call})
# print(messages)
# file=open("static/messages.json","a")
# file.write(str(messages) )
# file.close()
helper.filen=[]