Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How does conversation history work? #380

Open
jimgoo opened this issue Dec 19, 2024 · 1 comment
Open

How does conversation history work? #380

jimgoo opened this issue Dec 19, 2024 · 1 comment

Comments

@jimgoo
Copy link

jimgoo commented Dec 19, 2024

Very fun project :)

I couldn't quite figure out from a quick first read of the code how to use conversation history. Like if I run this, it doesn't know what I just asked:

r = lf.query('Who is Larry Page', lm=lm)
print(r)
# Larry Page is a prominent American computer scientist ...

r2 = lf.query('What did I just ask?', lm=lm)
print(r2)
# I'm sorry, but I can't recall or access previous interactions. 
@jimgoo
Copy link
Author

jimgoo commented Dec 20, 2024

Might have answered my own question, but let me know if there is a more preferred method:

import langfun as lf
from langfun.core.templates.conversation import Conversation

class Answer(pg.Object):
  result: int

lm = lf.llms.Gpt4o()

c = Conversation(
  preamble='You are a helpful and joyful chat bot. Let us chat.',
)

input_message = 'The result of one plus two is'
r = lf.query(c.prompt.render(input_message=input_message), Answer, lm=lm, returns_message=True)
# Answer(result=3)

# update memory
c.add(input_message, r)

input_message = 'What did I just say?'
r2 = lf.query(c.prompt.render(input_message=input_message), lm=lm, returns_message=True)
# "you just asked me the result of one plus two..."

It seems like under the hood, when making the call to the LLM provider, this Conversation template puts all of the turns into one single message with the user role instead of multiple messages with alternating roles?

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

No branches or pull requests

1 participant