Skip to content

Commit

Permalink
Skip LlamaGuard if GROQ_API_KEY is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaC215 committed Sep 2, 2024
1 parent 11fffe4 commit fa99122
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions agent/llama_guard.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from langchain_core.messages import AnyMessage, HumanMessage, AIMessage
from langchain_core.prompts import PromptTemplate
from langchain_groq import ChatGroq
Expand Down Expand Up @@ -75,10 +76,16 @@ def parse_llama_guard_output(output: str) -> LlamaGuardOutput:

class LlamaGuard:
def __init__(self):
if os.getenv("GROQ_API_KEY") is None:
print("GROQ_API_KEY not set, skipping LlamaGuard")
self.model = None
return
self.model = ChatGroq(model="llama-guard-3-8b", temperature=0.0)
self.prompt = PromptTemplate.from_template(llama_guard_instructions)

async def ainvoke(self, role: str, messages: List[AnyMessage]) -> LlamaGuardOutput:
if self.model is None:
return LlamaGuardOutput(safety_assessment=SafetyAssessment.SAFE)
role_mapping = {"ai": "Agent", "human": "User"}
messages_str = [
f"{role_mapping[m.type]}: {m.content}" for m in messages if m.type in ["ai", "human"]
Expand Down

0 comments on commit fa99122

Please sign in to comment.