Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ExpandedNodes": [
""
],
"SelectedNode": "\\ChatApp.sln",
"PreviewInSolutionExplorer": false
}
Binary file added .vs/ai-hub-apps/v17/.wsuo
Binary file not shown.
3 changes: 3 additions & 0 deletions apps/windows/cpp/ChatServer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.vs/
/ARM64/
/ChatServer/
7 changes: 7 additions & 0 deletions apps/windows/cpp/ChatServer/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files.associations": {
"xlocale": "cpp",
"xlocmes": "cpp",
"vector": "cpp"
}
}
132 changes: 132 additions & 0 deletions apps/windows/cpp/ChatServer/ChatServer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// ---------------------------------------------------------------------
// Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
// ---------------------------------------------------------------------
#include "ChatServer.hpp"
#include "PromptHandler.hpp"
#include <fstream>
#include <iostream>

using namespace App;

namespace
{

constexpr const int c_chat_separater_length = 80;

//
// ChatSplit - Line to split during Chat for UX
// Adds split line to separate out sections in output.
//
void ChatSplit(bool end_line = true)
{
std::string split_line(c_chat_separater_length, '-');
std::cout << "\n" << split_line;
if (end_line)
{
std::cout << "\n";
}
}

//
// GenieCallBack - Callback to handle response from Genie
// - Captures response from Genie into user_data
// - Print response to stdout
// - Add ChatSplit upon sentence completion
//
void GenieCallBack(const char* response_back, const GenieDialog_SentenceCode_t sentence_code, const void* user_data)
{
std::string* user_data_str = static_cast<std::string*>(const_cast<void*>(user_data));
user_data_str->append(response_back);

// Write user response to output.
std::cout << response_back;
if (sentence_code == GenieDialog_SentenceCode_t::GENIE_DIALOG_SENTENCE_END)
{
ChatSplit(false);
}
}

} // namespace

ChatServer::ChatServer(const std::string& config)
{
// Create Genie config
if (GENIE_STATUS_SUCCESS != GenieDialogConfig_createFromJson(config.c_str(), &m_config_handle))
{
throw std::runtime_error("Failed to create the Genie Dialog config. Please check config.");
}

// Create Genie dialog handle
if (GENIE_STATUS_SUCCESS != GenieDialog_create(m_config_handle, &m_dialog_handle))
{
throw std::runtime_error("Failed to create the Genie Dialog.");
}
}

ChatServer::~ChatServer()
{
if (m_config_handle != nullptr)
{
if (GENIE_STATUS_SUCCESS != GenieDialogConfig_free(m_config_handle))
{
std::cerr << "Failed to free the Genie Dialog config.";
}
}

if (m_dialog_handle != nullptr)
{
if (GENIE_STATUS_SUCCESS != GenieDialog_free(m_dialog_handle))
{
std::cerr << "Failed to free the Genie Dialog.";
}
}
}

void ChatServer::ChatLoop()
{
// AppUtils::PromptHandler prompt_handler;

// Initiate Chat with infinite loop.
// User to provide `exit` as a prompt to exit.
while (true)
{
std::string user_prompt;
std::string model_response;

// Input user prompt
ChatSplit();
std::cout << "Input: ";
std::getline(std::cin, user_prompt);

// Exit prompt

if (user_prompt.compare(c_exit_prompt) == 0)
{
std::cout << "Exiting chat per user's request.";
return;
}
// User provides an empty prompt
if (user_prompt.empty())
{
std::cout << "\nPlease enter prompt.\n";
continue;
}

// std::string tagged_prompt = prompt_handler.GetPromptWithTag(user_prompt);
std::string tagged_prompt = user_prompt;

// Bot's response
std::cout << "Output: ";
if (GENIE_STATUS_SUCCESS != GenieDialog_query(m_dialog_handle, tagged_prompt.c_str(),
GenieDialog_SentenceCode_t::GENIE_DIALOG_SENTENCE_COMPLETE,
GenieCallBack, &model_response))
{
throw std::runtime_error("Failed to get response from GenieDialog. Please restart the ChatServer.");
}
}
}

GenieDialog_Handle_t ChatServer::GetDialogHandle() const {
return m_dialog_handle;
}
53 changes: 53 additions & 0 deletions apps/windows/cpp/ChatServer/ChatServer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// ---------------------------------------------------------------------
// Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
// ---------------------------------------------------------------------
#pragma once

#include <string>

#include "GenieCommon.h"
#include "GenieDialog.h"

namespace App
{
constexpr const std::string_view c_exit_prompt = "exit";

class ChatServer
{
private:
GenieDialogConfig_Handle_t m_config_handle = nullptr;
GenieDialog_Handle_t m_dialog_handle = nullptr;
std::string m_user_name;

public:
/**
* ChatServer: Initializes ChatServer
* - Uses provided Genie configuration string
* - Creates handle for Genie
*
* @param config: JSON string containing Genie configuration
*
* @throws on failure to create handle for Genie config, dialog
*
*/
ChatServer(const std::string& config);
ChatServer() = delete;
ChatServer(const ChatServer&) = delete;
ChatServer(ChatServer&&) = delete;
ChatServer& operator=(const ChatServer&) = delete;
ChatServer& operator=(ChatServer&&) = delete;
~ChatServer();

/**
* ChatWithUser: Starts Chat with user using previously loaded config
*
* @param user_name: User name to use during chat
*
* @throws on failure to query model response during chat
*
*/
void ChatLoop();
GenieDialog_Handle_t GetDialogHandle() const;
};
} // namespace App
24 changes: 24 additions & 0 deletions apps/windows/cpp/ChatServer/ChatServer.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35219.272
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ChatServer", "ChatServer.vcxproj", "{6AECF6D0-1B11-483D-A9ED-44026305F3BE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM64 = Debug|ARM64
Release|ARM64 = Release|ARM64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6AECF6D0-1B11-483D-A9ED-44026305F3BE}.Debug|ARM64.ActiveCfg = Debug|ARM64
{6AECF6D0-1B11-483D-A9ED-44026305F3BE}.Debug|ARM64.Build.0 = Debug|ARM64
{6AECF6D0-1B11-483D-A9ED-44026305F3BE}.Release|ARM64.ActiveCfg = Release|ARM64
{6AECF6D0-1B11-483D-A9ED-44026305F3BE}.Release|ARM64.Build.0 = Release|ARM64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3EEC2CD0-F0DA-42ED-970E-1440F93328A7}
EndGlobalSection
EndGlobal
122 changes: 122 additions & 0 deletions apps/windows/cpp/ChatServer/ChatServer.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|ARM64">
<Configuration>Debug</Configuration>
<Platform>ARM64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|ARM64">
<Configuration>Release</Configuration>
<Platform>ARM64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{6aecf6d0-1b11-483d-a9ed-44026305f3be}</ProjectGuid>
<RootNamespace>ChatServer</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
<IncludePath>$(QNN_SDK_ROOT)\include\Genie;$(VC_IncludePath);$(WindowsSDK_IncludePath)</IncludePath>
<PublicIncludeDirectories>$(QNN_SDK_ROOT)\include\Genie</PublicIncludeDirectories>
<LibraryPath>$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
<LibraryPath>$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(QNN_SDK_ROOT)\include\Genie</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>$(QNN_SDK_ROOT)\lib\aarch64-windows-msvc\Genie.lib</AdditionalDependencies>
<DelayLoadDLLs>
</DelayLoadDLLs>
</Link>
<PostBuildEvent>
<Command>powershell "Copy-Item '$(QNN_SDK_ROOT)\lib\hexagon-v73\unsigned\*' '$(OutDir)' -Force"

powershell "Copy-Item '$(QNN_SDK_ROOT)\lib\aarch64-windows-msvc\*' '$(OutDir)' -Force"</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(QNN_SDK_ROOT)\include\Genie</AdditionalIncludeDirectories>
<AdditionalUsingDirectories>
</AdditionalUsingDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>$(QNN_SDK_ROOT)\lib\aarch64-windows-msvc\Genie.lib</AdditionalDependencies>
<DelayLoadDLLs>
</DelayLoadDLLs>
</Link>
<PostBuildEvent>
<Command>powershell cp $(QNN_SDK_ROOT)\lib\hexagon-v73\unsigned\* $(OutDir)
powershell cp $(QNN_SDK_ROOT)\lib\aarch64-windows-msvc\* $(OutDir)</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="ChatServer.cpp" />
<ClCompile Include="Main.cpp" />
<ClCompile Include="PromptHandler.cpp" />
<ClCompile Include="HttpServer.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="ChatServer.hpp" />
<ClInclude Include="PromptHandler.hpp" />
<ClInclude Include="HttpServer.hpp" />
</ItemGroup>
<ItemGroup>
<None Include="README.md" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
41 changes: 41 additions & 0 deletions apps/windows/cpp/ChatServer/ChatServer.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="ChatServer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="PromptHandler.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ChatServer.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="PromptHandler.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="README.md">
<Filter>Source Files</Filter>
</None>
</ItemGroup>
</Project>
Loading