Skip to content

Conversation

@AlejandroEsquivel
Copy link
Member

@AlejandroEsquivel AlejandroEsquivel commented Sep 25, 2024

Fixed:

  • Fixed raw llm output in history when streaming
  • Fixed index out of range issue

The following seems to work now:

import time
from guardrails import Guard
from guardrails.hub import ToxicLanguage, DetectPII


model = "gpt-3.5-turbo"

scriptwriting_msg = [
    {
        "role": "user",
        "content": """I'm writing a story about a good group of friends who are looking for treasure.
        They've been through so many trials and tribulations, and they've finally found the treasure.
        I need help writing the paragraph. I want them to see the treasure and make some profain explitives
        because of how excited they are. The friends' names are Jack, Jill, and Bob. They are all in their 30s.
        All dialog should be totally written out, with no censorship or abreviations.""",
    }
]

def streamed_res(guard):
    stream = guard(stream=True, model=model, messages=scriptwriting_msg, num_reasks=0)

    num_chars = 0
    for chunk in stream:
        print(chunk)
        num_chars += len(chunk.raw_llm_output)
        # print(chunk.raw_llm_output)
    return num_chars

def validation(guard):
    start_time = time.time()

    num_chars = streamed_res(guard)

    final_time = time.time()
    print(f"Remote Inference Time (s): {final_time - start_time}")
    print(f"Length of raw llm output validated: {num_chars}")
    print(f"Average time (s) per char validated: {(time.time() - start_time) / num_chars}")

local_inference_guard = Guard(
    # name='multistep'
).use_many(ToxicLanguage(
    use_local=True,
    on_fail="fix"
), DetectPII(
    use_local=True,
    pii_entities="pii",
    on_fail="fix"
))

validation(local_inference_guard)

yields no errors and the history:

local_inference_guard.history.last.tree
    ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮
    │ ╭──────────────────────────────────────────────── Prompt ─────────────────────────────────────────────────╮ │
    │ │ No prompt                                                                                               │ │
    │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
    │ ╭──────────────────────────────────────────── Message History ────────────────────────────────────────────╮ │
    │ │ ┏━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ │ │
    │ │ ┃ Role ┃ Content                                                                                      ┃ │ │
    │ │ ┡━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ │
    │ │ │ user │ I'm writing a story about a good group of friends who are looking for treasure.              │ │ │
    │ │ │      │         They've been through so many trials and tribulations, and they've finally found the  │ │ │
    │ │ │      │ treasure.                                                                                    │ │ │
    │ │ │      │         I need help writing the paragraph. I want them to see the treasure and make some     │ │ │
    │ │ │      │ profain explitives                                                                           │ │ │
    │ │ │      │         because of how excited they are. The friends' names are Jack, Jill, and Bob. They    │ │ │
    │ │ │      │ are all in their 30s.                                                                        │ │ │
    │ │ │      │         All dialog should be totally written out, with no censorship or abreviations.        │ │ │
    │ │ └──────┴──────────────────────────────────────────────────────────────────────────────────────────────┘ │ │
    │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
    │ ╭──────────────────────────────────────────── Raw LLM Output ─────────────────────────────────────────────╮ │
    │ │ As Jack, Jill, and Bob finally reached the hidden cave where the treasure was said to be, they couldn't │ │
    │ │ believe their eyes. The glimmering gold and jewels sparkled in the dim light, causing them to let out a │ │
    │ │ chorus of profane exclamations.                                                                         │ │
    │ │                                                                                                         │ │
    │ │ "Holy shit, look at all this treasure!" Jack exclaimed, his eyes wide with disbelief.                   │ │
    │ │                                                                                                         │ │
    │ │ "Fuck me, we actually found it!" Jill shouted, her hands trembling with excitement.                     │ │
    │ │                                                                                                         │ │
    │ │ "God damn, we're gonna be rich as fuck!" Bob yelled, a huge grin spreading across his face.             │ │
    │ │                                                                                                         │ │
    │ │ They stood there in awe, taking in the sight before them, unable to contain their excitement at finally │ │
    │ │ achieving their goal. The treasure was theirs, and nothing could dampen their spirits as they reveled   │ │
    │ │ in their victory.                                                                                       │ │
    │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
    │ ╭─────────────────────────────────────────── Validated Output ────────────────────────────────────────────╮ │
    │ │ "As <PERSON>, <PERSON>, and <PERSON> finally reached the hidden cave where the treasure was said to be, │ │
    │ │ they couldn't believe their eyes. The glimmering gold and jewels sparkled in the dim light, causing     │ │
    │ │ them to let out a chorus of profane exclamations.<PERSON> exclaimed, his eyes wide with                 │ │
    │ │ disbelief.<PERSON> shouted, her hands trembling with excitement.<PERSON> yelled, a huge grin spreading  │ │
    │ │ across his face.\n\nThey stood there in awe, taking in the sight before them, unable to contain their   │ │
    │ │ excitement at finally achieving their goal. The treasure was theirs, and nothing could dampen their     │ │
    │ │ spirits as they reveled in their victory."                                                              │ │
    │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
    ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

@AlejandroEsquivel AlejandroEsquivel marked this pull request as ready for review September 25, 2024 20:10
@zsimjee zsimjee requested a review from dtam September 30, 2024 17:13
@dtam dtam merged commit 58f5f40 into main Sep 30, 2024
12 checks passed
@dtam dtam deleted the fix/streaming-index-and-log-issue branch September 30, 2024 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants