-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(context managers): added Context Managers to help with tracing #83
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
Merged
shahules786
merged 6 commits into
explodinggradients:main
from
jjmachan:feat/context-managers
Aug 2, 2023
Merged
feat(context managers): added Context Managers to help with tracing #83
shahules786
merged 6 commits into
explodinggradients:main
from
jjmachan:feat/context-managers
Aug 2, 2023
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jjmachan
commented
Aug 2, 2023
Comment on lines
+96
to
+142
with trace_as_chain_group( | ||
callback_group_name, callback_manager=callbacks | ||
) as batch_group: | ||
for q, a in zip(question, answer): | ||
human_prompt = LONG_FORM_ANSWER_PROMPT.format(question=q, answer=a) | ||
prompts.append(ChatPromptTemplate.from_messages([human_prompt])) | ||
|
||
result = generate(prompts, self.llm, callbacks=batch_group) | ||
list_statements: list[list[str]] = [] | ||
for output in result.generations: | ||
# use only the first generation for each prompt | ||
statements = output[0].text.split("\n") | ||
list_statements.append(statements) | ||
|
||
prompts = [] | ||
for context, statements in zip(contexts, list_statements): | ||
statements_str: str = "\n".join( | ||
[f"{i+1}.{st}" for i, st in enumerate(statements)] | ||
) | ||
score = score / len(list_statements[i]) | ||
else: | ||
score = max(0, output.count("verdict: no")) / len(list_statements[i]) | ||
|
||
scores.append(1 - score) | ||
|
||
return ds.add_column(f"{self.name}", scores) # type: ignore | ||
contexts_str: str = "\n".join(context) | ||
human_prompt = NLI_STATEMENTS_MESSAGE.format( | ||
context=contexts_str, statements=statements_str | ||
) | ||
prompts.append(ChatPromptTemplate.from_messages([human_prompt])) | ||
|
||
result = generate(prompts, self.llm, callbacks=batch_group) | ||
outputs = result.generations | ||
|
||
scores = [] | ||
final_answer = "Final verdict for each statement in order:" | ||
final_answer = final_answer.lower() | ||
for i, output in enumerate(outputs): | ||
output = output[0].text.lower().strip() | ||
if output.find(final_answer) != -1: | ||
output = output[output.find(final_answer) + len(final_answer) :] | ||
score = sum( | ||
0 if "yes" in answer else 1 | ||
for answer in output.strip().split(".") | ||
if answer != "" | ||
) | ||
score = score / len(list_statements[i]) | ||
else: | ||
score = max(0, output.count("verdict: no")) / len(list_statements[i]) | ||
|
||
scores.append(1 - score) | ||
|
||
return scores |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you make sure this part is consistent with your change also. There was a merge conflict I fixed here
shahules786
approved these changes
Aug 2, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
todo
Added a context manager to group together runs in order to make it easier to visualise what is happening inside ragas.
future steps