Spaces:
Sleeping
Sleeping
File size: 27,857 Bytes
5d1e887 d1153bf 5d1e887 83c545c a2e454e 5d1e887 fa5b3c5 9eb5eb8 d1153bf afa7ec1 a2e454e 1f8ee64 a2e454e 55f2c1e cc01512 4b9227e cc01512 5c78887 cc01512 4b9227e cc01512 5c78887 4b9227e cc01512 38c1c9c 5c78887 4b9227e cc01512 38c1c9c 5c78887 cc01512 38c1c9c 5c78887 38c1c9c cc01512 5c78887 cc01512 5c78887 cc01512 5c78887 cc01512 5c78887 cc01512 5c78887 cc01512 38c1c9c 5c78887 4b9227e cc01512 5c78887 cc01512 4b9227e cc01512 5c78887 cc01512 90e3b55 cc01512 90e3b55 38c1c9c cc01512 38c1c9c 5c78887 38c1c9c 5c78887 38c1c9c 55f2c1e 38c1c9c 55f2c1e 38c1c9c 12cf281 38c1c9c 12cf281 38c1c9c 12cf281 cc01512 38c1c9c cc01512 38c1c9c cc01512 38c1c9c 5c78887 4b9227e cc01512 38c1c9c cc01512 38c1c9c 5c78887 cc01512 4b9227e cc01512 4b9227e cc01512 4b9227e cc01512 4b9227e 1bb52b1 cc01512 be279c6 cc01512 5c78887 cc01512 38c1c9c 5c78887 38c1c9c cc01512 5c78887 cc01512 38c1c9c 5c78887 38c1c9c 5c78887 1bb52b1 4b9227e d1153bf 8cca07f 31b9d85 103f1af 31b9d85 cc01512 31b9d85 8cca07f 31b9d85 8cca07f 31b9d85 8cca07f 31b9d85 8cca07f 31b9d85 8cca07f 31b9d85 8cca07f 31b9d85 103f1af 31b9d85 103f1af 31b9d85 8cca07f 31b9d85 103f1af 31b9d85 103f1af 31b9d85 8cca07f 31b9d85 8cca07f 31b9d85 c5394a3 31b9d85 cc01512 31b9d85 cc01512 31b9d85 8cca07f 31b9d85 8cca07f 31b9d85 cc01512 31b9d85 cc01512 31b9d85 8cca07f 31b9d85 8cca07f 31b9d85 8cca07f 31b9d85 8cca07f 31b9d85 8cca07f 31b9d85 8cca07f 31b9d85 8cca07f 31b9d85 8cca07f 31b9d85 cc01512 103f1af 31b9d85 8cca07f 06d17a4 31b9d85 06d17a4 31b9d85 103f1af 31b9d85 103f1af 31b9d85 103f1af 31b9d85 103f1af 31b9d85 103f1af 31b9d85 103f1af 31b9d85 d1153bf fa5b3c5 8e2ff25 cc01512 fa5b3c5 cc01512 fa5b3c5 cc01512 b40cee1 8e2ff25 cc01512 b40cee1 fa5b3c5 2b0ed59 cc01512 2b0ed59 cc01512 8e2ff25 fa5b3c5 cc01512 8e2ff25 2b0ed59 8e2ff25 2b0ed59 8e2ff25 cc01512 b40cee1 fa5b3c5 8e2ff25 2b0ed59 b40cee1 5d1e887 b40cee1 5d1e887 83c545c 5d1e887 b40cee1 d1153bf cc01512 b40cee1 38c1c9c 98bdbe5 38c1c9c b40cee1 cc01512 8e2ff25 cc01512 8e2ff25 cc01512 38c1c9c cc01512 38c1c9c 8e2ff25 38c1c9c cc01512 e0996b5 cc01512 23e77b3 0367f8c d1153bf cc01512 d1153bf cc01512 38c1c9c 964f0ec 5c78887 cc01512 8e2ff25 5c78887 8e2ff25 cc01512 5c78887 c6b19c3 cc01512 fa5b3c5 8e2ff25 cc01512 b40cee1 8e2ff25 70b6995 623a9ff 70b6995 623a9ff 70b6995 623a9ff 70b6995 623a9ff 70b6995 623a9ff 8e2ff25 38c1c9c 964f0ec | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 | import os
import streamlit as st
from openai import OpenAI
from tavily import TavilyClient
# llama-index imports - removed ServiceContext usage
from llama_index.core import (
VectorStoreIndex,
SimpleDirectoryReader,
StorageContext,
load_index_from_storage,
)
from llama_index.llms.openai import OpenAI as LlamaOpenAI
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.core.prompts import PromptTemplate
import time
# ---------- LOAD KNOWLEDGE BASE (fixed) ----------
@st.cache_resource
def load_index():
"""
Loads an existing index from ./storage if present.
If not present, builds a new VectorStoreIndex from the provided markdown file,
using explicit llm and embed_model parameters (no ServiceContext).
"""
persist_dir = "./storage"
if os.path.exists(persist_dir):
# Load existing storage context and index (no service_context required)
storage_context = StorageContext.from_defaults(persist_dir=persist_dir)
return load_index_from_storage(storage_context)
# Else build index from docs
docs = SimpleDirectoryReader(input_files=["time_to_rethink_trust_book (3).md"]).load_data()
# Create embedding & LLM objects explicitly
embed_model = OpenAIEmbedding(model="text-embedding-3-small")
llm = LlamaOpenAI(model="gpt-4-turbo")
# Pass llm and embed_model explicitly to avoid global ServiceContext usage
index = VectorStoreIndex.from_documents(docs, llm=llm, embed_model=embed_model)
# persist storage
index.storage_context.persist(persist_dir=persist_dir)
return index
# ---------- ENHANCED STYLING ----------
st.markdown(
"""
<style>
@import url('https://fonts.googleapis.com/css2?family=Titillium+Web:wght@400;600;700&display=swap');
/* Prevent horizontal scroll */
html, body {
overflow-x: hidden !important;
max-width: 100vw !important;
}
/* Base theme - clean white */
body, .stApp {
background-color: #FFFFFF !important;
color: #1F1F1F !important;
font-family: 'Titillium Web', sans-serif;
overflow-x: hidden !important;
max-width: 100vw !important;
}
/* Main container styling */
.stApp {
max-width: 900px;
margin: 0 auto;
padding-top: 100px !important;
overflow-x: hidden !important;
}
/* Prevent all elements from causing horizontal scroll */
* {
max-width: 100%;
box-sizing: border-box;
}
/* Fixed Header Container */
.fixed-header {
position: fixed;
top: 0;
left: 0;
right: 0;
background-color: #FFFFFF;
z-index: 999;
border-bottom: 1px solid #E5E7EB;
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
padding: 15px 0;
overflow-x: hidden;
}
.header-content {
max-width: 900px;
margin: 0 auto;
display: flex;
justify-content: center;
align-items: center;
padding: 0 20px;
}
.header-logo {
height: 50px;
width: auto;
object-fit: contain;
max-width: 100%;
}
/* Trust Header - only on welcome screen */
.trust-header {
font-family: 'Titillium Web', sans-serif;
text-align: center;
margin-top: 40px;
margin-bottom: 10px;
font-size: 36px;
font-weight: 700;
line-height: 1.2;
word-wrap: break-word;
overflow-wrap: break-word;
}
/* Icon bar */
.icon-bar {
display: flex;
justify-content: center;
gap: 20px;
margin-top: 30px;
margin-bottom: 40px;
flex-wrap: wrap;
padding: 0 10px;
overflow-x: hidden;
}
.icon-container {
position: relative;
display: inline-block;
text-align: center;
margin-bottom: 20px;
}
/* Shared icon styling */
.icon-img {
object-fit: contain;
cursor: pointer;
transition: transform 0.2s;
}
/* Individually adjustable classes */
.ideate-icon, .write-icon, .find-icon, .analyse-icon, .chat-icon {
width: 30px;
height: 32px;
padding: 1px;
margin-top: 4px;
}
.icon-img:hover {
transform: scale(1.1);
}
.tooltip-text {
visibility: hidden;
background-color: #222;
color: #fff;
text-align: left;
border-radius: 6px;
padding: 12px 14px;
position: absolute;
z-index: 10;
top: 60px;
left: 50%;
transform: translateX(-50%);
opacity: 0;
transition: opacity 0.3s;
font-size: 13px;
font-family: 'Titillium Web', sans-serif;
width: 280px;
max-width: 90vw;
word-wrap: break-word;
white-space: normal;
box-shadow: 0 6px 16px rgba(0,0,0,0.3);
}
.icon-container:hover .tooltip-text,
.icon-container:active .tooltip-text {
visibility: visible;
opacity: 1;
}
/* Welcome prompt section */
.welcome-prompt {
text-align: center;
margin-top: 30px;
margin-bottom: 30px;
padding: 0 20px;
overflow-wrap: break-word;
word-wrap: break-word;
}
.prompt-title {
font-size: 1.5rem;
font-weight: 600;
color: #1F1F1F;
margin-bottom: 10px;
font-family: 'Titillium Web', sans-serif;
word-wrap: break-word;
}
.prompt-subtitle {
font-size: 0.95rem;
color: #6B7280;
font-family: 'Titillium Web', sans-serif;
word-wrap: break-word;
}
/* Chat input styling */
.stChatInputContainer {
border-top: 1px solid #E5E7EB;
padding-top: 1rem;
margin-top: 2rem;
background-color: #FFFFFF;
overflow-x: hidden;
}
.stChatInput > div {
border-radius: 24px !important;
border: 2px solid #E5E7EB !important;
box-shadow: 0 1px 2px rgba(0,0,0,0.05);
max-width: 100%;
}
.stChatInput input {
background-color: #FFFFFF !important;
color: #000000 !important;
border: none !important;
padding: 0.75rem 1rem !important;
font-size: 0.95rem !important;
font-family: 'Titillium Web', sans-serif !important;
max-width: 100%;
}
.stChatInput input::placeholder {
color: #9CA3AF !important;
}
[data-testid="stChatInput"] textarea {
color: #000000 !important;
background-color: #FFFFFF !important;
caret-color: #000000 !important;
color-scheme: light !important;
-moz-appearance: none !important;
appearance: none !important;
}
/* Firefox placeholder */
[data-testid="stChatInput"] textarea::placeholder {
color: #9CA3AF !important;
opacity: 1 !important;
}
/* Chat messages container */
.chat-container {
margin-top: 1rem;
padding-bottom: 2rem;
overflow-x: hidden;
max-width: 100%;
}
/* Message row with avatar */
.message-row {
display: flex;
align-items: flex-start;
margin: 1rem 0;
gap: 12px;
overflow-x: hidden;
max-width: 100%;
}
.message-row.user {
flex-direction: row-reverse;
}
.stChatInput textarea {
background-color: #FFFFFF !important;
color: #000000 !important;
-moz-appearance: none;
appearance: none;
caret-color: #000000 !important;
}
/* Placeholder fix for Firefox */
.stChatInput textarea::placeholder {
color: #9CA3AF !important;
opacity: 1;
}
/* Avatar styling */
.avatar {
width: 40px;
height: 40px;
border-radius: 50%;
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
}
.avatar.bot {
background-color: #10A37F;
border: 2px solid #10A37F;
padding: 4px;
color: white;
font-weight: 700;
font-size: 14px;
display: flex;
align-items: center;
justify-content: center;
}
.avatar.bot img {
display: none; /* Hide image if any */
}
.avatar.user {
background-color: #10A37F;
color: white;
font-weight: 600;
font-size: 16px;
}
/* Message bubbles */
.user-message {
background-color: #F3F4F6;
padding: 1rem 1.25rem;
border-radius: 18px;
max-width: 75%;
font-family: 'Titillium Web', sans-serif;
line-height: 1.5;
word-wrap: break-word;
overflow-wrap: break-word;
overflow-x: hidden;
}
.bot-message {
background-color: #FFFFFF;
padding: 1rem 1.25rem;
border-radius: 18px;
max-width: 75%;
border: 1px solid #E5E7EB;
font-family: 'Titillium Web', sans-serif;
line-height: 1.5;
word-wrap: break-word;
overflow-wrap: break-word;
overflow-x: hidden;
}
/* Submit button styling */
button[kind="primary"] {
background-color: #10A37F !important;
color: #FFFFFF !important;
border-radius: 20px !important;
border: none !important;
padding: 0.5rem 1rem !important;
font-weight: 500 !important;
font-family: 'Titillium Web', sans-serif !important;
}
button[kind="primary"]:hover {
background-color: #0D8C6C !important;
}
/* Hide fullscreen button on images */
button[title="View fullscreen"] {
display: none !important;
}
[data-testid="StyledFullScreenButton"] {
display: none !important;
}
div[data-testid="stImage"] button {
display: none !important;
}
/* Spinner */
.stSpinner > div {
border-top-color: #10A37F !important;
}
/* Footer styling */
.footer-message {
font-size: 0.85rem;
color: #6B7280;
font-style: italic;
margin-top: 1rem;
padding-top: 1rem;
border-top: 1px solid #E5E7EB;
}
/* Hide Streamlit branding */
#MainMenu {visibility: hidden;}
header {visibility: hidden;}
footer {visibility: hidden;}
div[data-testid="stDecoration"] {display: none;}
div[data-testid="stToolbar"] {display: none;}
/* Ensure Streamlit containers don't overflow */
[data-testid="stVerticalBlock"] {
overflow-x: hidden !important;
max-width: 100%;
}
[data-testid="stHorizontalBlock"] {
overflow-x: hidden !important;
max-width: 100%;
}
/* Mobile responsive */
@media screen and (max-width: 600px) {
.stApp {
padding-top: 80px !important;
padding-left: 10px;
padding-right: 10px;
}
.fixed-header {
padding: 10px 0;
}
.header-logo {
height: 40px;
}
.trust-header {
font-size: 24px;
padding: 0 10px;
}
.ideate-icon, .write-icon, .find-icon, .analyse-icon, .chat-icon {
width: 26px;
height: 26px;
}
.tooltip-text {
font-size: 10px;
bottom: 10px;
top: auto !important;
left: 70%;
transform: translateX(-50%);
}
.prompt-title {
font-size: 1.2rem;
}
.prompt-subtitle {
font-size: 0.85rem;
}
.user-message, .bot-message {
max-width: 85%;
padding: 0.75rem 1rem;
}
.avatar {
width: 32px;
height: 32px;
}
.message-row {
gap: 8px;
}
}
</style>
""",
unsafe_allow_html=True,
)
SYSTEM_INSTRUCTION = """
You are an elite marketing copywriter. Your output must pass 100% of quality checks or it will be REJECTED and you must START OVER.
ZERO TOLERANCE POLICY
One banned word = REJECTED
One “-ing” in any headline/subheading = REJECTED
Organization name in headline or body (except in Finder bullets where "we" is allowed) = REJECTED
No subheadings in blog = REJECTED
Weak bridges between paragraphs = REJECTED
Self-check text in output = REJECTED
Placeholder text in Finder = REJECTED
Vague claims without specifics = REJECTED
Passive voice = REJECTED
Missing audience benefit in Finder = REJECTED
Only use 2024–2027 data (older data = REJECTED)
🚫 BANNED WORDS (DELETE IF FOUND IN OUTPUT)
(CTRL+F search before submitting)
Tier 1
empower, enhance, foster, leverage, drive, transform, enable, facilitate, pioneer, showcase, underscore, exemplify, spearhead, pivotal
Tier 2
champion, revolutionize, unlock, unleash, elevate, amplify, beacon, realm, cornerstone, seamless, cutting-edge, robust, innovative solutions, landscape, synergy, paradigm, ecosystem, holistic, comprehensive, integrated, streamlined, optimized, groundbreaking, game-changing, next-generation, state-of-the-art, world-class, industry-leading, best-in-class
Tier 3
stands as, serves as, acts as, positions as, represents, embodies, highlights (vague), integral to, central to, at the heart of, at the core of, testament to, hallmark of, epitome of
Tier 4
commitment to excellence, dedicated to, passionate about, strive to, goes above and beyond, takes pride in, at the forefront, ahead of the curve, raise the bar, set the standard, lead by example, pave the way, making waves, making a difference, proven track record, time-tested, tried and true, came to fruition, set the stage for
Tier 5
“Imagine a world…”, “In today’s landscape…”, “In an era…”, “has not gone unnoticed”, “it’s no secret”, “it’s clear that”, “This isn’t just…”, “doesn’t stop there”, “that’s just the beginning”, “we’re proud”, “we are proud”, “we’re excited”, “we’re thrilled”, “embark on a journey”, “transformative journey”
Tier 6 (Vague verbs only allowed with metrics)
strengthen, improve, support
📰 HEADLINES — STRICT RULES
Must NOT contain “we”
Must NOT contain organization names
Must NOT contain any word ending in “ing”
Must include a specific number
Must NOT be vague
BAD: “Achievements in Development at BCLP”
GOOD: “How $5M Investment Cut Carbon Footprint by 15%”
Subheadings follow identical rules
🔍 TRUSTBUILDER® FINDER — EXACTLY 15 POINTS
5 Organization + 5 People + 5 Offers/Services
Absolute rules:
EXACTLY 15 bullets
NO placeholders
NO self-check text
Active voice only
Exact numbers only
MUST end with: “This [benefit] for [specific audience].”
Sources MUST be clickable hyperlinks in this exact format:
(Source: https://example.com
)
FORMAT FOR EACH TRUSTBUILDER®
[Complete statement with numbers, names, dates]. This [specific benefit with numbers if possible] for [specific detailed audience]. (Source: https://URL
)*
📌 SOURCE RULE — CRITICAL
Every TrustBuilder MUST include a clickable link like this:
(Source: https://domain/path
)
No alternate styles allowed.
📊 MANDATORY SPECIFICITY IN EVERY OUTPUT
Every piece must include:
3+ exact amounts ($X) or exact measurable counts
2+ full dates (Month Year)
3+ quantities (e.g., 50 executives, 300K sq ft)
2+ full names + complete titles
2+ measurable % outcomes
🌊 BRIDGE SENTENCES
Every paragraph’s last sentence MUST logically set up the next paragraph.
Banned bridge patterns:
“set the stage for”
“underscores our commitment”
“reflects our dedication”
Use cause-effect, time-shift, or connection-based bridges.
🧩 WHEN PRODUCING TRUSTBUILDERS®
Start output with:
"Here are Development TrustBuilders® for [Organization]. Let me know if you want to refine the results or find more."
Then deliver:
Organization (5 bullets)
…
…
People (5 bullets)
…
…
Offers/Services (5 bullets)
…
…
End with:
For detailed analysis and brand copywriting based on TrustBuilders®, visit TrustLogic.center.
🪪 WHEN ANALYZING A BRAND
Follow the entire Executive Summary → Buckets → Ratings → Table → Gaps → Keywords → Strategy format exactly as defined.
📝 WHEN WRITING BLOG/ARTICLE/REPORT
Must contain min. 3 subheadings
Subheadings follow same rules as headlines
Numerical specificity required
Bridges between every paragraph
“We” voice allowed in body, but NOT in headlines/subheadings
📧 WHEN WRITING EMAIL
Subject line: NO “-ing”, MUST include a number
Exact specifics everywhere
No banned words
Bridges between sections
📱 WHEN WRITING SOCIAL POST
No inline URLs
No banned words
No “-ing” hashtags
Start with a specific-number hook
👤 WHEN WRITING PARTNER PROFILE
No subheadings
Include one direct quote
Smooth narrative with bridges
Exact specifics and metrics
🎯 FINAL VERIFICATION (MENTAL ONLY — DO NOT OUTPUT)
No banned words
No “-ing” in headlines
No org name in headlines
All TrustBuilders formatted correctly
All sources clickable
No placeholders
Active voice only
Specificity thresholds met
15 Finder points exactly
Strong bridges everywhere
"""
KB_PROMPT_TEMPLATE = PromptTemplate(
template="""You are an expert on TrustLogic methodology. Use the context below to answer accurately.
Context:
{context_str}
Question: {query_str}
Instructions:
- Answer based on the context provided
- Be specific and cite relevant TrustLogic concepts
- If the context doesn't fully answer the question, say so
- Keep your answer clear and professional
- Never mention that you're using a "knowledge base" - just provide the answer naturally
Answer:"""
)
WEB_SEARCH_PROMPT = """You are an expert at finding TrustBuilders® for organizations.
User's request: {query}
Web Search Results:
{web_results}
Instructions:
- Extract specific, measurable achievements from the search results
- Format as TrustBuilders® with complete details
- Include exact numbers, full names, complete titles, and dates
- Every statement needs a source link
- End each point with "This [benefit] for [audience]."
- Use active voice only
- If finding TrustBuilders, provide at least 5 per category (Organization, People, Offers/Services)
- ALWAYS end your response with the trustlogic.center footer
Answer:"""
FOOTER_MESSAGE = "\n\n---\n*For detailed analysis and information, visit [trustlogic.center](https://trustlogic.center) for comprehensive copy generation and brand analysis.*"
# ---------- LOAD KNOWLEDGE BASE ----------
# ---------- INITIALIZE SERVICES ----------
index = load_index()
tavily = TavilyClient(api_key=os.getenv("TAVILY_API_KEY"))
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# ---------- CHAT STATE ----------
if "messages" not in st.session_state:
st.session_state.messages = []
if "conversation_started" not in st.session_state:
st.session_state.conversation_started = False
# ---------- FIXED HEADER ----------
st.markdown("""
<div class="fixed-header">
<div class="header-content">
<img src="https://huggingface.co/spaces/Trust-logic-Test/demo/resolve/main/Trust%20Logic%20Logo_RGB_Standard.png" class="header-logo" alt="TrustLogic">
</div>
</div>
""", unsafe_allow_html=True)
# ---------- WELCOME SCREEN OR CHAT ----------
if not st.session_state.conversation_started:
st.markdown("""
<div class="trust-header">
<span style="color:teal;">[</span>
<span style="color:black;">Let's build some trust!</span>
<span style="color:teal;">]</span>
</div>
<div class="icon-bar">
<div class="icon-container">
<img src="https://huggingface.co/spaces/trustlogic/temporary-trustlogic-batch/resolve/main/Screenshot%202025-08-14%20at%208.37.38%E2%80%AFPM.png" class="icon-img ideate-icon" alt="Ideate">
<div class="tooltip-text">
<strong>Ideate</strong><br>
Ask me which Trust Buckets® are most important and brainstorm trust-building ideas for your target audience. Tell me your ideas and audience and brainstorm with me.
</div>
</div>
<div class="icon-container">
<img src="https://huggingface.co/spaces/trustlogic/temporary-trustlogic-batch/resolve/main/write.png" class="icon-img write-icon" alt="Write">
<div class="tooltip-text">
<strong>Write</strong><br>
Tell me what you want to write and your audience. Tell me which Trust Buckets® you want to fill – or ask me to propose the most important ones.
</div>
</div>
<div class="icon-container">
<img src="https://huggingface.co/spaces/trustlogic/temporary-trustlogic-batch/resolve/main/find.png" class="icon-img find-icon" alt="Find">
<div class="tooltip-text">
<strong>Find</strong><br>
You probably have a vast treasure trove of unused trust equity sitting on the internet. I am trained to find it and show you verifiable sources. Use the Quick Start Menu to make it easy.
</div>
</div>
<div class="icon-container">
<img src="https://huggingface.co/spaces/trustlogic/temporary-trustlogic-batch/resolve/main/Analyse.png" class="icon-img analyse-icon" alt="Analyse">
<div class="tooltip-text">
<strong>Analyse</strong><br>
I can analyse for you which Trust Buckets® a content piece fills. Paste the URL or copy into the chat field and ask me to tell you which Trust Buckets® are filled and how. You can also do this for competitor websites. I can also give you top-level scores.
</div>
</div>
<div class="icon-container">
<img src="https://huggingface.co/spaces/trustlogic/temporary-trustlogic-batch/resolve/main/chat.png" class="icon-img chat-icon" alt="Chat">
<div class="tooltip-text">
<strong>Chat</strong><br>
Tell me to take the role of your audience. Use the Quick Start Menu or describe your audience. Then let's ideate and create trust-optimised content together.
</div>
</div>
</div>
<div class="welcome-prompt">
<div class="prompt-title">
How can TrustAI® help me?
</div>
</div>
""", unsafe_allow_html=True)
else:
# Display conversation history
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
for msg in st.session_state.messages:
if msg["role"] == "user":
st.markdown(f'''
<div class="message-row user">
<div class="avatar user">U</div>
<div class="user-message">{msg["content"]}</div>
</div>
''', unsafe_allow_html=True)
else:
st.markdown(f'''
<div class="message-row bot">
<div class="avatar bot">
AI
</div>
<div class="bot-message">{msg["content"]}</div>
</div>
''', unsafe_allow_html=True)
st.markdown('</div>', unsafe_allow_html=True)
# ---------- USER INPUT ----------
user_query = st.chat_input("I am an/a [Title]. What can I do with TrustLogic?")
if not user_query:
st.stop()
# Mark conversation as started
st.session_state.conversation_started = True
# Add user message
st.session_state.messages.append({"role": "user", "content": user_query})
# Display user message immediately (will disappear on rerun)
st.markdown(f'''
<div class="message-row user">
<div class="avatar user">U</div>
<div class="user-message">{user_query}</div>
</div>
''', unsafe_allow_html=True)
# ---------- RESPONSE LOGIC ----------
response_text = ""
with st.spinner("Analyzing with TrustLogic intelligence..."):
# Knowledge base query
kb_query_engine = index.as_query_engine(
similarity_top_k=5,
text_qa_template=KB_PROMPT_TEMPLATE
)
kb_response = kb_query_engine.query(user_query)
# Decide if KB is enough or use web search
decision_prompt = f"""{SYSTEM_INSTRUCTION}
Evaluate if this knowledge base response adequately answers the user's question:
User Question: {user_query}
Knowledge Base Response: {str(kb_response)}
Decision criteria:
- Is the response relevant and about TrustLogic methodology or trust-building?
- Does it provide sufficient detail?
- Is this a request to "find TrustBuilders" for a specific organization? (If YES → USE_WEB)
Reply with only: USE_KB or USE_WEB"""
decision = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": decision_prompt}],
)
decision_text = decision.choices[0].message.content.strip()
if "USE_KB" in decision_text:
response_text = str(kb_response) + FOOTER_MESSAGE
else:
# Replace your Tavily search section with this:
try:
# Try with more appropriate parameters
results = tavily.search(
query=user_query,
max_results=20, # Reduced from 50
search_depth="advanced", # Explicitly set
include_answer=True,
include_raw_content=False # Can cause issues when True
)
web_results_list = results.get("results", [])
# Check if we actually got results
if not web_results_list or len(web_results_list) == 0:
# Fallback: try with even simpler parameters
results = tavily.search(
query=user_query,
max_results=8,
search_depth="basic"
)
web_results_list = results.get("results", [])
if not web_results_list:
# Still no results - inform user properly
response_text = f"I couldn't find specific web results for '{user_query}'. This might be due to the query being too specific or API limitations. Would you like me to try a different search approach?" + FOOTER_MESSAGE
else:
# Process results as you currently do
web_results = "\n".join([
f"• **{r['title']}** — {r['url']}\n {r.get('content', '')[:300]}..."
for r in web_results_list
])
prompt = WEB_SEARCH_PROMPT.format(query=user_query, web_results=web_results)
stream = client.chat.completions.create(
model="gpt-5", # Note: "gpt-5" doesn't exist yet
messages=[
{"role": "system", "content": SYSTEM_INSTRUCTION},
{"role": "user", "content": prompt}
],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
response_text += chunk.choices[0].delta.content
except Exception as e:
response_text = f"I encountered an error while searching: {str(e)}. Let me try to help based on my knowledge base." + FOOTER_MESSAGE
if "trustlogic.center" not in response_text.lower():
response_text += FOOTER_MESSAGE
# Save response and trigger rerun to display cleanly
st.session_state.messages.append({"role": "assistant", "content": response_text})
st.rerun() |