Skip to content

Commit 33a94d1

Browse files
committed
Add new sample chatbot
1 parent ba81640 commit 33a94d1

File tree

7 files changed

+1362
-0
lines changed

7 files changed

+1362
-0
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ else()
4747
add_subdirectory(speculative-simple)
4848
add_subdirectory(tokenize)
4949
add_subdirectory(xbapp)
50+
add_subdirectory(chatbot)
5051
if (NOT GGML_BACKEND_DL)
5152
# these examples use the backends directly and cannot be built with dynamic loading
5253
add_subdirectory(convert-llama2c-to-ggml)

examples/chatbot/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
set(TARGET chatbot)
2+
add_executable(${TARGET} main.cpp command.cpp hist.cpp)
3+
target_include_directories(${TARGET} PRIVATE ${CMAKE_SOURCE_DIR}/examples/chatbot/ ${CMAKE_SOURCE_DIR}/examples/chatbot/chatbot.inc/)
4+
install(TARGETS ${TARGET} RUNTIME)
5+
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})
6+
target_compile_features(${TARGET} PRIVATE cxx_std_11)

examples/chatbot/chatbot.h

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
#pragma once
2+
3+
#include "llama.h"
4+
#include "common.h"
5+
#include "console.h"
6+
#include "sampling.h"
7+
#include "log.h"
8+
#include "arg.h"
9+
10+
#ifdef GGML_USE_OPENMP
11+
#include <omp.h>
12+
#endif
13+
14+
#include <cmath>
15+
#include <cstdio>
16+
#include <cstdarg>
17+
#include <fstream>
18+
#include <iostream>
19+
#include <sstream>
20+
#include <string>
21+
#include <vector>
22+
#include <thread>
23+
#include <filesystem>
24+
#include <algorithm>
25+
26+
#ifdef _WIN32
27+
#if !defined WIN32_LEAN_AND_MEAN
28+
#define WIN32_LEAN_AND_MEAN
29+
#endif // WIN32_LEAN_AND_MEAN
30+
#ifndef NOMINMAX
31+
#define NOMINMAX
32+
#endif
33+
#include <windows.h>
34+
#include <fcntl.h>
35+
#include <io.h>
36+
#include <stdint.h>
37+
#endif
38+
39+
typedef volatile LONG atomic_int;
40+
inline void atomic_store(atomic_int * ptr, LONG val) {
41+
InterlockedExchange(ptr, val);
42+
}
43+
inline LONG atomic_load(atomic_int * ptr) {
44+
return InterlockedCompareExchange(ptr, 0, 0);
45+
}
46+
47+
#define DEFAULT_SYSTEM_PROMPT \
48+
"A chat between a curious human and an artificial intelligence assistant. " \
49+
"The assistant gives helpful, detailed, and polite answers to the " \
50+
"human's questions."
51+
52+
//struct clip_ctx;
53+
struct common_params;
54+
struct llama_context;
55+
struct llama_model;
56+
57+
enum Role {
58+
ROLE_USER,
59+
ROLE_ASSISTANT,
60+
ROLE_SYSTEM,
61+
ROLE_UNKNOWN,
62+
};
63+
64+
enum SpecialToken {
65+
IMAGE_PLACEHOLDER_TOKEN = -31337,
66+
};
67+
68+
extern bool g_manual_mode;
69+
extern bool g_said_something;
70+
extern char g_last_printed_char;
71+
//extern clip_ctx *g_clip;
72+
extern enum Role g_role;
73+
extern common_params g_params;
74+
extern int g_system_prompt_tokens;
75+
extern llama_context *g_ctx;
76+
extern llama_model *g_model;
77+
extern std::vector<int> g_history;
78+
extern atomic_int g_got_sigint;
79+
80+
int main(int, char **);
81+
82+
bool eval_string(std::string_view, bool, bool);
83+
bool eval_token(int);
84+
bool eval_tokens(std::vector<int>);
85+
bool handle_command(const char *);
86+
bool is_base_model();
87+
bool out_of_context(int);
88+
char *on_hint(const char *, const char **, const char **);
89+
const char *get_role_color(enum Role);
90+
const char *get_role_name(enum Role);
91+
enum Role cycle_role(enum Role);
92+
enum Role get_next_role(enum Role);
93+
int tokens_used(void);
94+
std::string token_to_piece(const llama_context *, int, bool);
95+
void adjust_stacks(int, int);
96+
void clear_ephemeral(void);
97+
void ensure_newline();
98+
void err(const char *, ...);
99+
void fix_stacks(void);
100+
void logo(char **);
101+
void on_clear(const std::vector<std::string> &);
102+
void on_context(const std::vector<std::string> &);
103+
void on_dump(const std::vector<std::string> &);
104+
void on_forget(const std::vector<std::string> &);
105+
void on_help(const std::vector<std::string> &);
106+
void on_manual(const std::vector<std::string> &);
107+
void on_pop(const std::vector<std::string> &);
108+
void on_push(const std::vector<std::string> &);
109+
void on_stack(const std::vector<std::string> &);
110+
void on_undo(const std::vector<std::string> &);
111+
void on_upload(const std::vector<std::string> &);
112+
void on_quit(const std::vector<std::string> &);
113+
void print(const std::string_view &);
114+
void print_ephemeral(const std::string_view &);
115+
void record_undo(void);
116+
void repl();
117+
void rewind(int);
118+
119+
#define RESET "\x1b[0m"
120+
#define BOLD "\x1b[1m"
121+
#define FAINT "\x1b[2m"
122+
#define UNBOLD "\x1b[22m"
123+
#define RED "\x1b[31m"
124+
#define GREEN "\x1b[32m"
125+
#define MAGENTA "\x1b[35m"
126+
#define YELLOW "\x1b[33m"
127+
#define CYAN "\x1b[36m"
128+
#define UNFOREGROUND "\x1b[39m"
129+
#define BRIGHT_BLACK "\x1b[90m"
130+
#define BRIGHT_RED "\x1b[91m"
131+
#define BRIGHT_GREEN "\x1b[92m"
132+
#define CLEAR_FORWARD "\x1b[K"
133+
134+
// Many llama.cpp APIs take boolean parameters at the end. Please favor
135+
// passing these constants as arguments instead, for better readability
136+
137+
#define ADD_SPECIAL true
138+
#define DONT_ADD_SPECIAL false
139+
140+
#define PARSE_SPECIAL true
141+
#define DONT_PARSE_SPECIAL false
142+
143+
#define ADD_ASSISTANT true
144+
#define DONT_ADD_ASSISTANT false
145+
146+
#define APPLY_GRAMMAR true
147+
#define DONT_APPLY_GRAMMAR false
148+
149+
#define RENDER_SPECIAL_TOKENS true
150+
#define DONT_RENDER_SPECIAL_TOKENS false
151+
152+
// same with llama_chat_message, but uses std::string
153+
struct llama_chat_msg {
154+
std::string role;
155+
std::string content;
156+
};
157+
158+
// CPP wrapper for llama_chat_apply_template
159+
// If the built-in template is not supported, we default to chatml
160+
// If the custom "tmpl" is not supported, we throw an error
161+
std::string llama_chat_apply_template(const struct llama_model * model,
162+
const std::string & tmpl,
163+
const std::vector<llama_chat_msg> & chat,
164+
bool add_ass);
165+
166+
// Format single message, while taking into account the position of that message in chat history
167+
std::string llama_chat_format_single(const struct llama_model * model,
168+
const std::string & tmpl,
169+
const std::vector<llama_chat_msg> & past_msg,
170+
const llama_chat_msg & new_msg,
171+
bool add_ass);
172+
173+
// Returns an example of formatted chat
174+
std::string llama_chat_format_example(const struct llama_model * model,
175+
const std::string & tmpl);
176+
177+
// replaces multiple isspace() with one space and trims result
178+
std::string collapse(const std::string_view &input);
179+
180+
std::string basename(const std::string_view &path);

0 commit comments

Comments
 (0)