Detect reasoning withing chat in ollama chat completion.

Fix minor bug in Http async.
This commit is contained in:
Martín Lucas Golini
2025-08-11 00:23:18 -03:00
parent a1959fd149
commit 1e95c07a1f
4 changed files with 31 additions and 13 deletions
-7
View File
@@ -1179,13 +1179,6 @@ void Http::AsyncRequest::run() {
eeSAFE_DELETE( mStream );
}
// The Async Request destroys the socket used to create the request
if ( mHttp->mConnection ) {
HttpConnection* connection = mHttp->mConnection;
eeSAFE_DELETE( connection );
mHttp->mConnection = NULL;
}
mRunning = false;
}
+11 -4
View File
@@ -931,13 +931,13 @@ LLMModel LLMChatUI::findModel( const std::string& provider, const std::string& m
return *modelIt;
}
}
if ( provider == "openai" && model == "gpt-4o" )
if ( provider == "openai" && model == "gpt-5" )
return mCurModel; // Do not stack-overflow if something is really wrong
return getDefaultModel();
}
LLMModel LLMChatUI::getDefaultModel() {
return findModel( "openai", "gpt-4o" );
return findModel( "openai", "gpt-5" );
}
std::string LLMChatUI::getNewFilePath( const std::string& uuid, const std::string& summary,
@@ -970,6 +970,12 @@ std::string LLMChatUI::prepareApiUrl( const std::string& apiKey ) {
return url;
}
static bool allNewLines( const std::string& s ) {
if ( s.empty() )
return false;
return std::all_of( s.begin(), s.end(), []( char c ) { return c == '\n'; } );
}
void LLMChatUI::doRequest() {
if ( mRequest )
return;
@@ -1003,7 +1009,8 @@ void LLMChatUI::doRequest() {
mRequest->streamedResponseCb = [this, editor, thinking, thinkingID]( const std::string& chunk,
bool fromReasoning ) {
if ( fromReasoning )
if ( fromReasoning ||
( mRequest && mRequest->getResponse().empty() && allNewLines( chunk ) ) )
return;
auto conversation = chunk;
editor->runOnMainThread(
@@ -1086,7 +1093,7 @@ void LLMChatUI::doRequest() {
String::trimInPlace( mSummary, ' ' );
String::trimInPlace( mSummary, '"' );
} else {
// TODO: Implement generating a summary base on the user prompt (take the
// TODO: Implement generating a summary based on the user prompt (take the
// first few words)
mSummary = i18n( "untitled_conversation", "Untitled Conversation" );
}
@@ -48,9 +48,21 @@ LLMChatCompletionRequest::LLMChatCompletionRequest( const std::string& uri, cons
std::string delta = msg["content"];
if ( delta.empty() )
return;
if ( mFirstMessage ) {
if ( String::startsWith( delta, "<think>" ) )
mReasoning = true;
mFirstMessage = false;
}
if ( streamedResponseCb )
streamedResponseCb( delta, false );
mResponse += std::move( delta );
streamedResponseCb( delta, mReasoning );
if ( mReasoning ) {
if ( String::endsWith( delta, "</think>" ) )
mReasoning = false;
mReasoningResponse += std::move( delta );
} else {
mResponse += std::move( delta );
}
}
}
@@ -70,6 +82,7 @@ LLMChatCompletionRequest::LLMChatCompletionRequest( const std::string& uri, cons
std::string delta = choice["delta"]["reasoning_content"];
if ( streamedResponseCb )
streamedResponseCb( delta, true );
mReasoningResponse += std::move( delta );
} else if ( choice["delta"].contains( "content" ) ) {
std::string delta = choice["delta"]["content"];
if ( streamedResponseCb )
@@ -40,14 +40,19 @@ class LLMChatCompletionRequest {
const std::string& getResponse() const { return mResponse; }
const std::string& getReasoningResponse() const { return mReasoningResponse; }
protected:
URI mUrl;
Http mHttp;
Http::Request mRequest;
IOStreamString mStream;
std::string mReasoningResponse;
std::string mResponse;
size_t mReadBytes{ 0 };
bool mCancel{ false };
bool mFirstMessage{ true };
bool mReasoning{ false };
};
} // namespace ecode