@@ -66,19 +66,27 @@ def inference(latest_message, history):
66
66
global BACKEND_INITIALISED
67
67
68
68
try :
69
+ context = []
69
70
if INCLUDE_SYSTEM_PROMPT :
70
- context = [ SystemMessage (content = settings .model_instruction )]
71
+ context . append ( SystemMessage (content = settings .model_instruction ))
71
72
else :
72
- context = []
73
- for i , (human , ai ) in enumerate (history ):
74
- if not INCLUDE_SYSTEM_PROMPT and i == 0 :
75
- # Mimic system prompt by prepending it to first human message
76
- human = f"{ settings .model_instruction } \n \n { human } "
77
- context .append (HumanMessage (content = human ))
78
- context .append (AIMessage (content = (ai or "" )))
73
+ # Mimic system prompt by prepending it to first human message
74
+ history [0 ]['content' ] = f"{ settings .model_instruction } \n \n { history [0 ]['content' ]} "
75
+
76
+ for message in history :
77
+ role = message ['role' ]
78
+ content = message ['content' ]
79
+ if role == "user" :
80
+ context .append (HumanMessage (content = content ))
81
+ else :
82
+ if role != "assistant" :
83
+ log .warn (f"Message role { role } converted to 'assistant'" )
84
+ context .append (AIMessage (content = (content or "" )))
79
85
context .append (HumanMessage (content = latest_message ))
86
+
80
87
log .debug ("Chat context: %s" , context )
81
88
89
+
82
90
response = ""
83
91
for chunk in llm .stream (context ):
84
92
# If this is our first successful response from the backend
@@ -109,6 +117,7 @@ def inference(latest_message, history):
109
117
raise gr .Error (ui_message )
110
118
111
119
except openai .APIConnectionError as err :
120
+ log .info (err )
112
121
if not BACKEND_INITIALISED :
113
122
log .info ("Backend API not yet ready" )
114
123
gr .Info (
0 commit comments