MCPcopy Create free account
hub / github.com/OpenBMB/ToolBench / stream

Function stream

toolbench/inference/toolbench_server.py:103–157  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

101@app.route('/stream', methods=['GET', 'POST'])
102@cross_origin()
103def stream():
104 data = json.loads(request.data)
105 user_input = data["text"]
106 top_k = data["top_k"]
107 method = data["method"]
108 print("Called stream")
109 global model
110
111 def generate(model):
112 print("Called generate")
113 if model.inuse:
114 # send 409 error
115 return Response(json.dumps({
116 "method_name": "error",
117 "error": "Model in use"
118 }), status=409, mimetype='application/json')
119 return
120 model.inuse = True
121
122 # run model.run_agent in the background
123 with concurrent.futures.ThreadPoolExecutor() as executor:
124
125 future = executor.submit(model.run_pipeline, user_input, method, top_k)
126 # keep waiting for the queue to be empty
127 while True:
128 if model.queue.empty():
129 if future.done():
130 print("Finished with future")
131 break
132 time.sleep(0.01)
133 continue
134 else:
135 obj = model.queue.get()
136 if obj["method_name"] == "unknown": continue
137 if obj["method_name"] == "on_request_end":
138 yield json.dumps(obj)
139 break
140
141 try:
142 yield json.dumps(obj) + "\n"
143 except Exception as e:
144 model.inuse = False
145 print(obj)
146 print(e)
147
148 try:
149 future.result()
150 except Exception as e:
151 model.inuse = False
152 print(e)
153
154 model.inuse = False
155 return
156
157 return Response(stream_with_context(generate(model)))
158
159@app.route('/methods', methods=['GET'])
160@cross_origin()

Callers

nothing calls this directly

Calls 1

generateFunction · 0.85

Tested by

no test coverage detected