Team Ai
Apppublic

KBaba7/llama.cpp

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
readme.md287 linesDownload Raw Back to public_simplechat
1 2# SimpleChat3 4by Humans for All.5 6## quickstart7 8To run from the build dir9 10bin/llama-server -m path/model.gguf --path ../examples/server/public_simplechat11 12Continue reading for the details.13 14## overview15 16This simple web frontend, allows triggering/testing the server's /completions or /chat/completions endpoints17in a simple way with minimal code from a common code base. Inturn additionally it tries to allow single or18multiple independent back and forth chatting to an extent, with the ai llm model at a basic level, with their19own system prompts.20 21This allows seeing the generated text / ai-model response in oneshot at the end, after it is fully generated,22or potentially as it is being generated, in a streamed manner from the server/ai-model.23 24![Chat and Settings screens](./simplechat_screens.webp "Chat and Settings screens")25 26Auto saves the chat session locally as and when the chat is progressing and inturn at a later time when you27open SimpleChat, option is provided to restore the old chat session, if a matching one exists.28 29The UI follows a responsive web design so that the layout can adapt to available display space in a usable30enough manner, in general.31 32Allows developer/end-user to control some of the behaviour by updating gMe members from browser's devel-tool33console. Parallely some of the directly useful to end-user settings can also be changed using the provided34settings ui.35 36NOTE: Current web service api doesnt expose the model context length directly, so client logic doesnt provide37any adaptive culling of old messages nor of replacing them with summary of their content etal. However there38is a optional sliding window based chat logic, which provides a simple minded culling of old messages from39the chat history before sending to the ai model.40 41NOTE: Wrt options sent with the request, it mainly sets temperature, max_tokens and optionaly stream for now.42However if someone wants they can update the js file or equivalent member in gMe as needed.43 44NOTE: One may be able to use this to chat with openai api web-service /chat/completions endpoint, in a very45limited / minimal way. One will need to set model, openai url and authorization bearer key in settings ui.46 47 48## usage49 50One could run this web frontend directly using server itself or if anyone is thinking of adding a built in web51frontend to configure the server over http(s) or so, then run this web frontend using something like python's52http module.53 54### running using examples/server55 56./llama-server -m path/model.gguf --path examples/server/public_simplechat [--port PORT]57 58### running using python3's server module59 60first run examples/server61* ./llama-server -m path/model.gguf62 63next run this web front end in examples/server/public_simplechat64* cd ../examples/server/public_simplechat65* python3 -m http.server PORT66 67### using the front end68 69Open this simple web front end from your local browser70 71* http://127.0.0.1:PORT/index.html72 73Once inside74 75* If you want to, you can change many of the default global settings76  * the base url (ie ip addr / domain name, port)77  * chat (default) vs completion mode78  * try trim garbage in response or not79  * amount of chat history in the context sent to server/ai-model80  * oneshot or streamed mode.81 82* In completion mode83  * one normally doesnt use a system prompt in completion mode.84  * logic by default doesnt insert any role specific "ROLE: " prefix wrt each role's message.85    If the model requires any prefix wrt user role messages, then the end user has to86    explicitly add the needed prefix, when they enter their chat message.87    Similarly if the model requires any prefix to trigger assistant/ai-model response,88    then the end user needs to enter the same.89    This keeps the logic simple, while still giving flexibility to the end user to90    manage any templating/tagging requirement wrt their messages to the model.91  * the logic doesnt insert newline at the begining and end wrt the prompt message generated.92    However if the chat being sent to /completions end point has more than one role's message,93    then insert newline when moving from one role's message to the next role's message, so94    that it can be clearly identified/distinguished.95  * given that /completions endpoint normally doesnt add additional chat-templating of its96    own, the above ensures that end user can create a custom single/multi message combo with97    any tags/special-tokens related chat templating to test out model handshake. Or enduser98    can use it just for normal completion related/based query.99 100* If you want to provide a system prompt, then ideally enter it first, before entering any user query.101  Normally Completion mode doesnt need system prompt, while Chat mode can generate better/interesting102  responses with a suitable system prompt.103  * if chat.add_system_begin is used104    * you cant change the system prompt, after it is has been submitted once along with user query.105    * you cant set a system prompt, after you have submitted any user query106  * if chat.add_system_anytime is used107    * one can change the system prompt any time during chat, by changing the contents of system prompt.108    * inturn the updated/changed system prompt will be inserted into the chat session.109    * this allows for the subsequent user chatting to be driven by the new system prompt set above.110 111* Enter your query and either press enter or click on the submit button.112  If you want to insert enter (\n) as part of your chat/query to ai model, use shift+enter.113 114* Wait for the logic to communicate with the server and get the response.115  * the user is not allowed to enter any fresh query during this time.116  * the user input box will be disabled and a working message will be shown in it.117  * if trim garbage is enabled, the logic will try to trim repeating text kind of garbage to some extent.118 119* just refresh the page, to reset wrt the chat history and or system prompt and start afresh.120 121* Using NewChat one can start independent chat sessions.122  * two independent chat sessions are setup by default.123 124* When you want to print, switching ChatHistoryInCtxt to Full and clicking on the chat session button of125  interest, will display the full chat history till then wrt same, if you want full history for printing.126 127 128## Devel note129 130### Reason behind this131 132The idea is to be easy enough to use for basic purposes, while also being simple and easily discernable133by developers who may not be from web frontend background (so inturn may not be familiar with template /134end-use-specific-language-extensions driven flows) so that they can use it to explore/experiment things.135 136And given that the idea is also to help explore/experiment for developers, some flexibility is provided137to change behaviour easily using the devel-tools/console or provided minimal settings ui (wrt few aspects).138Skeletal logic has been implemented to explore some of the end points and ideas/implications around them.139 140 141### General142 143Me/gMe consolidates the settings which control the behaviour into one object.144One can see the current settings, as well as change/update them using browsers devel-tool/console.145It is attached to the document object. Some of these can also be updated using the Settings UI.146 147  baseURL - the domain-name/ip-address and inturn the port to send the request.148 149  bStream - control between oneshot-at-end and live-stream-as-its-generated collating and showing150  of the generated response.151 152    the logic assumes that the text sent from the server follows utf-8 encoding.153 154    in streaming mode - if there is any exception, the logic traps the same and tries to ensure155    that text generated till then is not lost.156 157      if a very long text is being generated, which leads to no user interaction for sometime and158      inturn the machine goes into power saving mode or so, the platform may stop network connection,159      leading to exception.160 161  apiEP - select between /completions and /chat/completions endpoint provided by the server/ai-model.162 163  bCompletionFreshChatAlways - whether Completion mode collates complete/sliding-window history when164  communicating with the server or only sends the latest user query/message.165 166  bCompletionInsertStandardRolePrefix - whether Completion mode inserts role related prefix wrt the167  messages that get inserted into prompt field wrt /Completion endpoint.168 169  bTrimGarbage - whether garbage repeatation at the end of the generated ai response, should be170  trimmed or left as is. If enabled, it will be trimmed so that it wont be sent back as part of171  subsequent chat history. At the same time the actual trimmed text is shown to the user, once172  when it was generated, so user can check if any useful info/data was there in the response.173 174    One may be able to request the ai-model to continue (wrt the last response) (if chat-history175    is enabled as part of the chat-history-in-context setting), and chances are the ai-model will176    continue starting from the trimmed part, thus allows long response to be recovered/continued177    indirectly, in many cases.178 179    The histogram/freq based trimming logic is currently tuned for english language wrt its180    is-it-a-alpabetic|numeral-char regex match logic.181 182  apiRequestOptions - maintains the list of options/fields to send along with api request,183  irrespective of whether /chat/completions or /completions endpoint.184 185    If you want to add additional options/fields to send to the server/ai-model, and or186    modify the existing options value or remove them, for now you can update this global var187    using browser's development-tools/console.188 189    For string, numeric and boolean fields in apiRequestOptions, including even those added by a190    user at runtime by directly modifying gMe.apiRequestOptions, setting ui entries will be auto191    created.192 193    cache_prompt option supported by example/server is allowed to be controlled by user, so that194    any caching supported wrt system-prompt and chat history, if usable can get used. When chat195    history sliding window is enabled, cache_prompt logic may or may not kick in at the backend196    wrt same, based on aspects related to model, positional encoding, attention mechanism etal.197    However system prompt should ideally get the benefit of caching.198 199  headers - maintains the list of http headers sent when request is made to the server. By default200  Content-Type is set to application/json. Additionally Authorization entry is provided, which can201  be set if needed using the settings ui.202 203  iRecentUserMsgCnt - a simple minded SlidingWindow to limit context window load at Ai Model end.204  This is disabled by default. However if enabled, then in addition to latest system message, only205  the last/latest iRecentUserMsgCnt user messages after the latest system prompt and its responses206  from the ai model will be sent to the ai-model, when querying for a new response. IE if enabled,207  only user messages after the latest system message/prompt will be considered.208 209    This specified sliding window user message count also includes the latest user query.210    <0 : Send entire chat history to server211     0 : Send only the system message if any to the server212    >0 : Send the latest chat history from the latest system prompt, limited to specified cnt.213 214 215By using gMe's iRecentUserMsgCnt and apiRequestOptions.max_tokens/n_predict one can try to control216the implications of loading of the ai-model's context window by chat history, wrt chat response to217some extent in a simple crude way. You may also want to control the context size enabled when the218server loads ai-model, on the server end.219 220 221Sometimes the browser may be stuborn with caching of the file, so your updates to html/css/js222may not be visible. Also remember that just refreshing/reloading page in browser or for that223matter clearing site data, dont directly override site caching in all cases. Worst case you may224have to change port. Or in dev tools of browser, you may be able to disable caching fully.225 226 227Currently the server to communicate with is maintained globally and not as part of a specific228chat session. So if one changes the server ip/url in setting, then all chat sessions will auto229switch to this new server, when you try using those sessions.230 231 232By switching between chat.add_system_begin/anytime, one can control whether one can change233the system prompt, anytime during the conversation or only at the beginning.234 235 236### Default setup237 238By default things are setup to try and make the user experience a bit better, if possible.239However a developer when testing the server of ai-model may want to change these value.240 241Using iRecentUserMsgCnt reduce chat history context sent to the server/ai-model to be242just the system-prompt, prev-user-request-and-ai-response and cur-user-request, instead of243full chat history. This way if there is any response with garbage/repeatation, it doesnt244mess with things beyond the next question/request/query, in some ways. The trim garbage245option also tries to help avoid issues with garbage in the context to an extent.246 247Set max_tokens to 1024, so that a relatively large previous reponse doesnt eat up the space248available wrt next query-response. However dont forget that the server when started should249also be started with a model context size of 1k or more, to be on safe side.250 251  The /completions endpoint of examples/server doesnt take max_tokens, instead it takes the252  internal n_predict, for now add the same here on the client side, maybe later add max_tokens253  to /completions endpoint handling code on server side.254 255NOTE: One may want to experiment with frequency/presence penalty fields in apiRequestOptions256wrt the set of fields sent to server along with the user query, to check how the model behaves257wrt repeatations in general in the generated text response.258 259A end-user can change these behaviour by editing gMe from browser's devel-tool/console or by260using the provided settings ui (for settings exposed through the ui).261 262 263### OpenAi / Equivalent API WebService264 265One may be abe to handshake with OpenAI/Equivalent api web service's /chat/completions endpoint266for a minimal chatting experimentation by setting the below.267 268* the baseUrl in settings ui269  * https://api.openai.com/v1 or similar270 271* Wrt request body - gMe.apiRequestOptions272  * model (settings ui)273  * any additional fields if required in future274 275* Wrt request headers - gMe.headers276  * Authorization (available through settings ui)277    * Bearer THE_OPENAI_API_KEY278  * any additional optional header entries like "OpenAI-Organization", "OpenAI-Project" or so279 280NOTE: Not tested, as there is no free tier api testing available. However logically this might281work.282 283 284## At the end285 286Also a thank you to all open source and open model developers, who strive for the common good.287