File size: 2,823 Bytes
7a8853f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
    try:
        del helper.data["imageURL"]
    except:
        pass

    try:
        del helper.data["context"]
    except:
        pass

    for msg in messages:
        if isinstance(msg["content"],list):
            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")



            msg["content"]=msg["content"][0]["text"]


    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=[]





def ask(query,prompt,api_endpoint,output={}):
  if output=={}:
    python_boolean_to_json = {
      "true": True,
    }

    data = {
        'jailbreakConversationId': json.dumps(python_boolean_to_json['true']),
        "systemMessage":prompt,
        "message":query,
        "toneStyle":"turbo",
        "plugins":{"search":False},
        #     "persona":"sydney"

    }
    try:
        data["imageURL"]=helper.data["imageURL"]
    except:
        pass

    resp=requests.post(api_endpoint, json=data,timeout=80) 

    return resp.json()["response"]
  else:
    resp=requests.post(api_endpoint, json=output) 
    return resp.json()