Skip to content

Commit

Permalink
feat(weave): simple prompt classes (#3032)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamie-rasmussen authored Nov 21, 2024
1 parent 370c6ec commit 98c54ca
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 378 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
411 changes: 102 additions & 309 deletions docs/docs/guides/core-types/prompts.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const sidebars: SidebarsConfig = {
"guides/evaluation/scorers",
],
},
// "guides/core-types/prompts",
"guides/core-types/prompts",
"guides/core-types/models",
"guides/core-types/datasets",
"guides/tracking/feedback",
Expand Down
29 changes: 14 additions & 15 deletions tests/trace/test_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@


def test_stringprompt_format():
class MyPrompt(StringPrompt):
def format(self, **kwargs) -> str:
return "Imagine a lot of complicated logic build this string."

prompt = MyPrompt()
assert prompt.format() == "Imagine a lot of complicated logic build this string."
prompt = StringPrompt("You are a pirate. Tell us your thoughts on {topic}.")
assert (
prompt.format(topic="airplanes")
== "You are a pirate. Tell us your thoughts on airplanes."
)


def test_messagesprompt_format():
class MyPrompt(MessagesPrompt):
def format(self, **kwargs) -> list:
return [
{"role": "user", "content": "What's 23 * 42"},
]

prompt = MyPrompt()
assert prompt.format() == [
{"role": "user", "content": "What's 23 * 42"},
prompt = MessagesPrompt(
[
{"role": "system", "content": "You are a pirate."},
{"role": "user", "content": "Tell us your thoughts on {topic}."},
]
)
assert prompt.format(topic="airplanes") == [
{"role": "system", "content": "You are a pirate."},
{"role": "user", "content": "Tell us your thoughts on airplanes."},
]
14 changes: 7 additions & 7 deletions weave-js/src/components/FancyPage/useProjectSidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ export const useProjectSidebar = (
key: 'dividerWithinWeave-2',
isShown: isWeaveOnly,
},
// {
// type: 'button' as const,
// name: 'Prompts',
// slug: 'weave/prompts',
// isShown: showWeaveSidebarItems || isShowAll,
// iconName: IconNames.ForumChatBubble,
// },
{
type: 'button' as const,
name: 'Prompts',
slug: 'weave/prompts',
isShown: showWeaveSidebarItems || isShowAll,
iconName: IconNames.ForumChatBubble,
},
{
type: 'button' as const,
name: 'Models',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,6 @@ export const TabUsePrompt = ({
const isParentObject = !ref.artifactRefExtra;
const label = isParentObject ? 'prompt version' : 'prompt';

// TODO: Simplify if no params.
const longExample = `import weave
from openai import OpenAI
weave.init("${projectName}")
${pythonName} = weave.ref("${uri}").get()
class MyModel(weave.Model):
model_name: str
prompt: weave.Prompt
@weave.op
def predict(self, params: dict) -> dict:
client = OpenAI()
response = client.chat.completions.create(
model=self.model_name,
messages=self.prompt.bind(params),
)
result = response.choices[0].message.content
if result is None:
raise ValueError("No response from model")
return result
mymodel = MyModel(model_name="gpt-3.5-turbo", prompt=${pythonName})
# Replace with desired parameter values
params = ${JSON.stringify({}, null, 2)}
print(mymodel.predict(params))
`;

return (
<Box className="text-sm">
<Alert icon="lightbulb-info">
Expand All @@ -85,15 +54,6 @@ print(mymodel.predict(params))
tooltipText="Click to copy unabridged string"
/>
</Box>

<Box mt={2}>A more complete example:</Box>
<Box mt={2}>
<CopyableText
text={longExample}
copyText={longExample}
tooltipText="Click to copy unabridged string"
/>
</Box>
</Box>
);
};
47 changes: 41 additions & 6 deletions weave/flow/prompt/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,52 @@ def color_content(content: str, values: dict) -> str:

class Prompt(Object):
def format(self, **kwargs: Any) -> Any:
raise NotImplemented
raise NotImplementedError("Subclasses must implement format()")


class MessagesPrompt(Prompt):
def format(self, **kwargs: Any) -> list:
raise NotImplemented
class StringPrompt(Prompt):
content: str = ""

def __init__(self, content: str):
super().__init__()
self.content = content

class StringPrompt(Prompt):
def format(self, **kwargs: Any) -> str:
raise NotImplemented
return self.content.format(**kwargs)

@staticmethod
def from_obj(obj: Any) -> "StringPrompt":
prompt = StringPrompt(content=obj.content)
prompt.name = obj.name
prompt.description = obj.description
return prompt


class MessagesPrompt(Prompt):
messages: list[dict] = Field(default_factory=list)

def __init__(self, messages: list[dict]):
super().__init__()
self.messages = messages

def format_message(self, message: dict, **kwargs: Any) -> dict:
m = {}
for k, v in message.items():
if isinstance(v, str):
m[k] = v.format(**kwargs)
else:
m[k] = v
return m

def format(self, **kwargs: Any) -> list:
return [self.format_message(m, **kwargs) for m in self.messages]

@staticmethod
def from_obj(obj: Any) -> "MessagesPrompt":
prompt = MessagesPrompt(messages=obj.messages)
prompt.name = obj.name
prompt.description = obj.description
return prompt


class EasyPrompt(UserList, Prompt):
Expand Down
12 changes: 12 additions & 0 deletions weave/trace/refs.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ def objectify(self, obj: Any) -> Any:
# version number or latest alias resolved to a specific digest.
prompt.__dict__["ref"] = obj.ref
return prompt
if "StringPrompt" == class_name:
from weave.flow.prompt.prompt import StringPrompt

prompt = StringPrompt.from_obj(obj)
prompt.__dict__["ref"] = obj.ref
return prompt
if "MessagesPrompt" == class_name:
from weave.flow.prompt.prompt import MessagesPrompt

prompt = MessagesPrompt.from_obj(obj)
prompt.__dict__["ref"] = obj.ref
return prompt
return obj

def get(self) -> Any:
Expand Down

0 comments on commit 98c54ca

Please sign in to comment.