Spaces:
Sleeping
Sleeping
Initial Commit
Browse files- Dockerfile +20 -0
- app.py +155 -0
- requirements.txt +5 -0
Dockerfile
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
RUN apt update
|
9 |
+
RUN apt install -y protobuf-compiler libprotobuf-dev wget
|
10 |
+
RUN useradd -m -u 1000 user
|
11 |
+
USER user
|
12 |
+
ENV HOME=/home/user \
|
13 |
+
PATH=/home/user/.local/bin:$PATH
|
14 |
+
|
15 |
+
WORKDIR $HOME/app
|
16 |
+
|
17 |
+
COPY --chown=user . $HOME/app
|
18 |
+
|
19 |
+
RUN wget https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct-GGUF/resolve/main/qwen2.5-0.5b-instruct-q4_k_m.gguf
|
20 |
+
CMD ["flask", "run", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
import json
|
3 |
+
from flask_cors import CORS
|
4 |
+
app = Flask(__name__)
|
5 |
+
CORS(app)
|
6 |
+
import urllib.parse
|
7 |
+
from llama_cpp import Llama
|
8 |
+
llm = Llama(model_path="qwen2.5-0.5b-instruct-q4_k_m.gguf", chat_format="chatml-function-calling")
|
9 |
+
|
10 |
+
|
11 |
+
class cget():
|
12 |
+
def __init__(self, object):
|
13 |
+
self.object = object
|
14 |
+
|
15 |
+
def get(self, subscript, exceptr):
|
16 |
+
try:
|
17 |
+
return self.object[subscript]
|
18 |
+
except:
|
19 |
+
return exceptr
|
20 |
+
|
21 |
+
def convert_openai_to_langchain(input):
|
22 |
+
additional_kwargs = None
|
23 |
+
try:
|
24 |
+
additional_kwargs = {
|
25 |
+
"tool_calls": [
|
26 |
+
{
|
27 |
+
"id": "No ID",
|
28 |
+
"function": {
|
29 |
+
"arguments": cget(z['function']).get('arguments', {}),
|
30 |
+
"name": cget(z['function']).get('name', '')
|
31 |
+
},
|
32 |
+
"type": "function"
|
33 |
+
}
|
34 |
+
for z in cget(input['choices'][0]['message']).get('tool_calls', [])
|
35 |
+
]
|
36 |
+
}
|
37 |
+
except:
|
38 |
+
additional_kwargs = None
|
39 |
+
|
40 |
+
try:
|
41 |
+
if len(cget(additional_kwargs).get('tool_calls', [])) < 1:
|
42 |
+
additional_kwargs = None
|
43 |
+
else:
|
44 |
+
pass
|
45 |
+
except:
|
46 |
+
additional_kwargs = None
|
47 |
+
|
48 |
+
json_data = {
|
49 |
+
"content": cget(cget(cget(input).get('choices', [])).get(0, {})).get('messages', {}).get('content', None),
|
50 |
+
"additional_kwargs": additional_kwargs,
|
51 |
+
"response_metadata": {
|
52 |
+
"token_usage": {
|
53 |
+
"completion_tokens": cget(cget(input).get('usage', None)).get('completion_tokens', None),
|
54 |
+
"prompt_tokens": cget(cget(input).get('usage', None)).get('prompt_tokens', None),
|
55 |
+
"total_tokens": cget(cget(input).get('usage', None)).get('total_tokens', None),
|
56 |
+
},
|
57 |
+
"model_name": cget(input).get('model', None),
|
58 |
+
},
|
59 |
+
"type": "ai",
|
60 |
+
"name": None,
|
61 |
+
"id": cget(input).get('id', None),
|
62 |
+
"example": False,
|
63 |
+
"usage_metadata": {
|
64 |
+
"input_tokens": cget(cget(input).get('usage', None)).get('prompt_tokens', None),
|
65 |
+
"output_tokens": cget(cget(input).get('usage', None)).get('completion_tokens', None),
|
66 |
+
"total_tokens": cget(cget(input).get('usage', None)).get('total_tokens', None)
|
67 |
+
},
|
68 |
+
"DirectResult": cget(cget(cget(input).get('choices', [])).get(0, {})).get('messages', {}).get('content', None),
|
69 |
+
"original_response":input,
|
70 |
+
}
|
71 |
+
return json_data
|
72 |
+
|
73 |
+
def ToolSelector(other):
|
74 |
+
|
75 |
+
AVAIABLETOOLS = [
|
76 |
+
{
|
77 |
+
"name": "search",
|
78 |
+
"description": "Search Internet For Related Query and Provide Uptodate query",
|
79 |
+
"parameters": {
|
80 |
+
"type": "object",
|
81 |
+
"properties": {
|
82 |
+
"query": {
|
83 |
+
"type": "string",
|
84 |
+
"description": "Search Query Follow the General Search Methods to get better result"
|
85 |
+
}
|
86 |
+
},
|
87 |
+
"required": ["query"]
|
88 |
+
}
|
89 |
+
}
|
90 |
+
]
|
91 |
+
toreturn = []
|
92 |
+
if(type(other['tools']) == list):
|
93 |
+
try:
|
94 |
+
tools = other['tools']
|
95 |
+
for tool in tools:
|
96 |
+
for thistool in AVAIABLETOOLS:
|
97 |
+
if(thistool['name'] == tool):
|
98 |
+
thistool['type'] = 'function'
|
99 |
+
thistool['function'] = {
|
100 |
+
"name":thistool['name'],
|
101 |
+
"parameters":thistool['parameters']
|
102 |
+
}
|
103 |
+
thistool.pop('parameters',None)
|
104 |
+
toreturn.append(thistool)
|
105 |
+
else:
|
106 |
+
pass
|
107 |
+
except:
|
108 |
+
pass
|
109 |
+
else:
|
110 |
+
raise Exception('tools is not provided in list formate')
|
111 |
+
print(toreturn)
|
112 |
+
return toreturn
|
113 |
+
|
114 |
+
def checknu(m):
|
115 |
+
return(m != None and m != '')
|
116 |
+
@app.route('/', methods=['POST','OPTIONS'])
|
117 |
+
def process_data():
|
118 |
+
if request.method == 'POST':
|
119 |
+
data = request.get_json()
|
120 |
+
if data:
|
121 |
+
if (not False in [checknu(data.get('conversation',None)),checknu(data.get('provider',None)),checknu(data.get('model',None)),checknu(data.get('api')),checknu(data.get('other',None))]):
|
122 |
+
conversation = data.get('conversation')
|
123 |
+
conversation = [{'role':z['role'],'content':z['context']}for z in conversation]
|
124 |
+
provider = data.get('provider')
|
125 |
+
model = data.get('model')
|
126 |
+
api = data.get('api')
|
127 |
+
other = data.get('other')
|
128 |
+
print(other)
|
129 |
+
tools=ToolSelector(other)
|
130 |
+
|
131 |
+
if(provider=="Flash-Tool"):
|
132 |
+
print(json.dumps({"tools":tools},indent=4))
|
133 |
+
toreturn = llm.create_chat_completion(
|
134 |
+
messages = conversation,
|
135 |
+
tools=tools,
|
136 |
+
tool_choice={
|
137 |
+
"type": "function",
|
138 |
+
"function": {
|
139 |
+
"name": "search"
|
140 |
+
}
|
141 |
+
}
|
142 |
+
)
|
143 |
+
return json.dumps(convert_openai_to_langchain(toreturn),indent=4)
|
144 |
+
else:
|
145 |
+
return json.dumps({'type':'error','message':'Flash-Tool is expected Provider'})
|
146 |
+
else:
|
147 |
+
return json.dumps({'type':'error','message':'missing parameter'},indent=4)
|
148 |
+
|
149 |
+
else:
|
150 |
+
return jsonify({"message": "No data received"}), 400
|
151 |
+
else:
|
152 |
+
return jsonify({"message": "Invalid request method"})
|
153 |
+
|
154 |
+
if __name__ == '__main__':
|
155 |
+
app.run(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
langchain_community
|
3 |
+
llama_cpp_python
|
4 |
+
flask
|
5 |
+
flask_cors
|