Skip to content

Instantly share code, notes, and snippets.

@mahiya
Created June 28, 2024 00:58
Show Gist options
  • Save mahiya/2c74d94cd05357b30a32a8a4ed709cd1 to your computer and use it in GitHub Desktop.
Save mahiya/2c74d94cd05357b30a32a8a4ed709cd1 to your computer and use it in GitHub Desktop.
RAGのHyDEにて仮回答を生成するためのPythonコードの案
def generate_hypo_answer(self, question: str) -> str:
"""
HyDE(仮説的文書埋め込み)のためにクエリ(質問)に対する仮説的回答を生成します。
Args:
question (str): 仮説的回答を生成するためのクエリ(質問)
Returns:
str: 生成された仮説的回答
"""
system_message = "You are an AI assistant that helps people find information."
user_message = f"""
Please write a passage to answer the question and output in JSON format.
Passage must be Japanese.
# Question
{question}
# Output Format
{{
"passage": "{{generated passage}}"
}}
"""
resp = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": user_message},
],
max_tokens=4096,
temperature=0,
response_format={"type": "json_object"}
)
completion = resp.choices[0].message.content
hypo_answer = json.loads(completion)["passage"]
return {"hypo_answer": hypo_answer}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment