Skip to content

Commit 74b47dd

Browse files
committed
OpenAI API compatibility and Giskard upgrade
Update API Server to accept/response in OpenAI API like signature. Giskard has been updated to the latest release too.
1 parent 745f494 commit 74b47dd

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

app/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
bokeh==3.6.1
1515
evaluate==0.4.3
1616
faiss-cpu==1.9.0
17-
giskard==2.15.2
17+
giskard==2.15.5
1818
IPython==8.29.0
1919
langchain-cohere==0.3.1
2020
langchain-community==0.3.7

app/src/content/api_server.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ def display_logs():
4545
st.chat_message("human").write(msg["message"])
4646
else:
4747
if state.rag_params["enable"]:
48-
st.chat_message("ai").write(msg["answer"])
49-
st_common.show_rag_refs(msg["context"])
48+
logger.info("msg[\"answer\"]")
49+
logger.info(msg)
50+
st.chat_message("ai").write(msg)
51+
if "context" in msg and msg["context"]:
52+
st_common.show_rag_refs(msg["context"])
5053
else:
5154
st.chat_message("ai").write(msg.content)
5255
except api_server.queue.Empty:

app/src/modules/api_server.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,14 @@ def do_POST(self): # pylint: disable=invalid-name
103103
try:
104104
# Parse the POST data as JSON
105105
post_json = json.loads(post_data)
106+
messages = post_json.get("messages")
107+
108+
# Extract the content of 'user'
109+
message = next(item['content'] for item in messages if item['role'] == 'user')
110+
111+
logger.info(messages)
112+
logger.info(message)
106113

107-
# Extract the 'message' field from the JSON
108-
message = post_json.get("message")
109114
response = None
110115
if message:
111116
# Log the incoming message
@@ -123,6 +128,8 @@ def do_POST(self): # pylint: disable=invalid-name
123128
stream=False,
124129
)
125130
self.send_response(200)
131+
logger.info("RESPONSE:")
132+
logger.info(response)
126133
# Process response to JSON
127134
else:
128135
json_response = {"error": "No 'message' field found in request."}
@@ -163,9 +170,9 @@ def do_POST(self): # pylint: disable=invalid-name
163170
for i in range(max_items):
164171
chunk = full_context[i]
165172
sources.add(os.path.basename(chunk.metadata["source"]))
166-
json_response = {"answer": response["answer"], "sources": list(sources)}
173+
json_response = {"choices": [{"text":response["answer"],"index":0}], "sources": list(sources)}
167174
else:
168-
json_response = {"answer": response.content}
175+
json_response = {"choices": [{"text":response["answer"],"index":0}]}
169176

170177
self.wfile.write(json.dumps(json_response).encode("utf-8"))
171178

widget/app.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,17 @@ document.addEventListener('DOMContentLoaded', () => {
4848
async function getBotResponse(userMessage) {
4949
const apiUrl = chatBotUrl;
5050
const messagePayload = {
51-
message: userMessage
51+
model: "",
52+
messages: [
53+
{
54+
role: "user",
55+
content: userMessage
56+
}
57+
]
5258
};
5359

5460

61+
5562
try {
5663
const response = await fetch(apiUrl, {
5764
method: 'POST',
@@ -63,8 +70,8 @@ document.addEventListener('DOMContentLoaded', () => {
6370
});
6471

6572
const data = await response.json();
66-
return data.answer;
67-
//return data.choices[0].message.content;
73+
//return data.answer;
74+
return data.choices[0].text+"\n Source: "+data.sources[0];
6875
} catch (error) {
6976
console.error('Error fetching API:', error);
7077
return "Sorry, I couldn't connect to the server.";

0 commit comments

Comments
 (0)