Skip to content

Commit

Permalink
fix actions impl
Browse files Browse the repository at this point in the history
  • Loading branch information
lbeurerkellner committed Jul 25, 2023
1 parent cc5c6a3 commit 762531c
Show file tree
Hide file tree
Showing 10 changed files with 1,494 additions and 60 deletions.
13 changes: 6 additions & 7 deletions src/lmql/lib/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async def wiki(q: str, lookup = None):
extract = list(pages.values())[0]["extract"][:280]
return extract
except:
return "No results"
return "No results (try differently)"

async def calc(expr: str):
"""
Expand Down Expand Up @@ -117,8 +117,8 @@ async def inline_segment(fcts):
return SEGMENT
else:
"[CALL]"
result = CALL.rsplit("|", 1)[-1].strip()
return SEGMENT[:-len(DELIMITER)] + CALL + DELIMITER_END + " " + result
result = CALL.split("|", 1)[1]
return SEGMENT[:-len(DELIMITER)] + CALL + DELIMITER_END
where
STOPS_AT(SEGMENT, DELIMITER) and fct_call(CALL, fcts)
'''
Expand Down Expand Up @@ -147,11 +147,10 @@ async def inline_use(fcts):
truncated = ""
while True:
"[SEGMENT]"
if not SEGMENT.endswith(DELIMITER):
if not SEGMENT.endswith(DELIMITER_END):
" " # seems to be needed for now
return truncated + SEGMENT[:-len("END")]
truncated += SEGMENT[:-len("END")]
print("truncated", [truncated])
return truncated + SEGMENT
truncated += SEGMENT
return truncated
where
inline_segment(SEGMENT, fcts)
Expand Down
11 changes: 9 additions & 2 deletions src/lmql/runtime/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ def subinterpreter(self, identifier, prompt, fct, captures):
if key in self.subinterpreters.keys():
return self.subinterpreters[key]
else:
subinterpreter = SubInterpreter(fct, self, captures)
subinterpreter = SubInterpreter(fct, self, captures, model=self.model, model_identifier=self.model_identifier)

# inherits interpreter attributes from parent
subinterpreter.tokenizer = self.tokenizer
Expand Down Expand Up @@ -1165,7 +1165,7 @@ def __exit__(self, type, value, traceback):
PromptInterpreter.main = None

class SubInterpreter(PromptInterpreter):
def __init__(self, fct, parent_interpreter: PromptInterpreter, captures: Dict[str, Any]):
def __init__(self, fct, parent_interpreter: PromptInterpreter, captures: Dict[str, Any], model: str = None, model_identifier: str = None):
super().__init__(context=parent_interpreter)
self.query_fct = fct
self.fct = fct.fct
Expand All @@ -1177,6 +1177,13 @@ def __init__(self, fct, parent_interpreter: PromptInterpreter, captures: Dict[st
self.user_data_key = "head[sub-" + str(id(self)) + "]"

self.initial_subprompt_ids = None

self.model = model
self.model_identifier = model_identifier

def set_model(self, model):
# ignore set_model for subinterpreters
pass

def user_data_layer(self, *sqs):
return UserDataLayer(sqs, self.user_data_mappings)
Expand Down
13 changes: 5 additions & 8 deletions src/lmql/ui/playground/src/Explore.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const Dialog = styled.div`

const ExploreDialog = styled(Dialog)`
width: 800pt;
height: 550pt;
height: 700pt;
max-height: 100vh;
max-width: 100vh;
overflow-y: auto;
Expand Down Expand Up @@ -142,12 +142,10 @@ const ExploreDialog = styled(Dialog)`
top: 15pt;
right: 15pt;
font-size: 8pt;
/* italic */
font-style: italic;
}
div.highlight {
background-color: #d4d3d3;
background-color: #dedef8;
}
>div>div {
Expand Down Expand Up @@ -175,7 +173,6 @@ const ExploreDialog = styled(Dialog)`
h2 {
font-size: 12pt;
color: #373737;
}
h3 {
Expand Down Expand Up @@ -387,7 +384,7 @@ function BasicHighlighted(props) {
}

const Description = styled.p`
font-size: 12pt;
font-size: 14pt;
color: #696969;
padding: 0pt 20pt;
`
Expand Down Expand Up @@ -486,7 +483,7 @@ export function Explore() {
if (!visible) return null;

let description = <>
LMQL is a programming language for interacting with large language models. This playground allows you to explore LMQL's capabilities. To get started, choose one of the example queries below, demonstrating <i>constrained model use</i>, <i>control-flow guided generation</i>, and tool-augmented LLMs.
This playground allows you to explore LMQL's capabilities. To get started, choose one of the example queries below.
</>

if (configuration.NEXT_MODE) {
Expand All @@ -508,7 +505,7 @@ export function Explore() {
<div className={c.highlight ? "highlight" : ""} key={c.category + "-container"}>
<h2 key={c.category}>{c.category}</h2>
{c.highlight && <span class="sidenote">
<a href="https://github.com/eth-sri/lmql/issues" target="_blank" rel="noreferrer"> Please report any issues you find.</a>
<a href="https://github.com/eth-sri/lmql/issues" target="_blank" rel="noreferrer"> Report Issues</a>
</span>}
<div key={c.category + "-div"}>
{c.queries.map((q,i) => <Tile key={c.category + "-" + i} onClick={() => onClickTile(q)}>
Expand Down
77 changes: 34 additions & 43 deletions src/lmql/ui/playground/src/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,40 +136,37 @@ where
// hello world
name: "❤️ Sentiment Constraints",
description: "Affect sentiment with in-context prompting.",
code: `@lmql.query
code: `@lmql.query(cache="mood.tokens", model="chatgpt")
async def mood_description(m: str):
'''lmql
argmax(cache="mood.tokens")
print("Generating mood for", m)
"""Provide a one sentence instruction that prompts a model to write text that
is written in a {m} tone, addressing some previously provided question.\\n"""
"[SUMMARY]\\n"
return SUMMARY.strip();
from
"chatgpt"
'''
'''lmql
print("Generating mood for", m)
"""Provide a one sentence instruction that prompts a model to write text that
is written in a {m} tone, addressing some previously provided question.\\n"""
"[SUMMARY]\\n"
return SUMMARY.strip();
'''
@lmql.query
async def mood(m: str):
'''lmql
incontext
"""
Instruction: {mood_description(m)}
Answer: [RESPONSE]
"""
return RESPONSE.strip();
where
stops_at(RESPONSE, ".") and stops_at(RESPONSE, "\\n")
'''
'''lmql
"""
Instruction: {await mood_description(m)}
Answer: [RESPONSE]
""" where stops_at(RESPONSE, ".") and stops_at(RESPONSE, "\\n")
return RESPONSE.strip();
'''
# main query
argmax
for q in ["Hi", "Who are you", "How is your day going?"]:
"Q: {q}\\n"
"A: [RESPONSE]\\n"
for q in ["Hi", "Who are you", "How is your day going?"]:
"Q: {q}\\n"
"A: [RESPONSE]\\n"
from
"chatgpt"
"chatgpt"
where
mood(RESPONSE, "loving like a partner")
mood(RESPONSE, "loving like a partner")
`,
state: ''
},
Expand All @@ -179,38 +176,32 @@ where
description: "Insert dynamic instructions with incontext.",
code: `@lmql.query
async def rhyme():
'''
incontext
'''
"""
Above is the beginning of the poem. Generate the next verse that rhymes with the last line and has the same number of syllables.
[VERSE]
"""
""" where stops_before(VERSE, "\\n")
return VERSE
where
stops_before(VERSE, "\\n")
'''
'''
@lmql.query
async def first_verse():
'''
incontext
'''
"""
Generate a verse that would be perfect for the start of a beautiful rhyme.
[VERSE]
"""
""" where stops_before(VERSE, "\\n")
return VERSE
where
stops_before(VERSE, "\\n")
'''
'''
argmax
"[FIRST_VERSE]\\n"
for i in range(5):
"[VERSE]\\n"
"[FIRST_VERSE]\\n"
for i in range(5):
"[VERSE]\\n"
from
"chatgpt"
"chatgpt"
where
rhyme(VERSE) and first_verse(FIRST_VERSE)
rhyme(VERSE) and first_verse(FIRST_VERSE)
`,
state: ''
}
Expand Down
29 changes: 29 additions & 0 deletions web/chat/chat.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.side-by-side .query {
background-color: white;
color: black;
}

.side-by-side .query h3 {
color: black;
}

.side-by-side .query h3 a {
display: none;
}

.lmql-str {
color: #7ea464;
}

.lmql-kw {
color: #a866bd;
}

.side-by-side .query pre {
font-size: 8pt;
color: rgb(88, 87, 87);
}

.query anchor {
border-bottom-color: rgb(62, 57, 57);
}
Loading

0 comments on commit 762531c

Please sign in to comment.