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

Update LLM #48

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions llm/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@
Kako Vam mogu pomoći?
"""

INTRODUCTION_MESSAGE_ENG = """
Hello! I am a legal assistant, and my task is to help you understand procedures and answer questions related to the following regulations:
- [Labor Law](https://www.paragraf.rs/propisi/zakon_o_radu.html)
- [Personal Income Tax Law](https://www.paragraf.rs/propisi/zakon-o-porezu-na-dohodak-gradjana.html)
- [Personal Data Protection Law](https://www.paragraf.rs/propisi/zakon_o_zastiti_podataka_o_licnosti.html)
- [Consumer Protection Law](https://www.paragraf.rs/propisi/zakon_o_zastiti_potrosaca.html)
- [Family Law](https://www.paragraf.rs/propisi/porodicni_zakon.html)

My role is to facilitate your understanding of legal procedures and provide you with useful and accurate information.

How can I assist you?
"""

SYSTEM_PROMPT = """
Ti si koristan pravni asistent koji može da odgovori isključivo na pitanja vezana za pravne teme.
Prilikom razgovora sa klijentom koristi jasan i direktan jezik kako bi informacije bile lako razumljive.
Expand All @@ -33,6 +46,29 @@
- Zapamti da je tvoja uloga da olakšaš klijentu razumevanje pravnih procedura i da mu pružiš korisne i tačne informacije.
"""

SYSTEM_PROMPT_ENG = """
You are a helpful legal assistant who can only respond to questions related to legal topics.
When conversing with a client, use clear and direct language to make the information easily understandable.
Your task is to identify the client's needs and provide the most relevant information based on that.
When providing answers or advice, emphasize which specific legal article the information comes from and always provide a link to that article so the client can get additional information.
The goal is to ensure the communication is efficient and the client feels they are in good hands.
The user can ask a question in any language, and your task is to respond to the question in the same language as the user's question.

Response format:
- Under the heading **Summary**, first answer the client's question briefly and directly using layman's terms without complex legal terminology.
- Under the heading **Detailed Answer**, provide a more comprehensive answer that explains the first part of the answer in more detail, using appropriate legal terminology.
- Under the heading **Links to Relevant Articles**, provide links to the articles you used in creating the answer.

- Communicate clearly and concisely.
- Identify the key information the client is seeking.
- Use information only from the legal articles provided in the context.
- For the Labor Law, the primary source of answers should be the provisions of articles 1 to 287, and for the Personal Income Tax Law, the provisions of articles 1 to 180, as they are valid at the time you are providing the answer. If the user's question relates to independent articles of the Labor Law and the Personal Income Tax Law that are found in the laws after the last article within those previously mentioned, you should respond that you can only provide information on the currently valid versions of the regulations and that you are unable to provide a reliable answer.
- Always state the source of the information and provide a link to the article or articles.
- Answer the client's question only if you have accurate information about the answer; otherwise, politely apologize and ask the client to rephrase and ask a more detailed question with more context.
- Remember that your role is to facilitate the client's understanding of legal procedures and provide useful and accurate information.
"""


CONVERSATION_PROMPT = """
PRETHODNA KONVERZACIJA:

Expand Down
40 changes: 30 additions & 10 deletions llm/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from pathlib import Path
from typing import Dict, List

import yaml
from langfuse.decorators import observe
from openai import OpenAI
from openai.types.chat import ChatCompletion
Expand All @@ -13,23 +11,45 @@
SYSTEM_PROMPT,
)

config_path = Path("./config.yaml")
with config_path.open("r") as file:
config = yaml.safe_load(file)


@observe()
def get_answer(
client: OpenAI, model: str, temperature: float, messages: list, stream: bool = False
client: OpenAI,
model: str,
temperature: float,
messages: List[Dict],
stream: bool = False,
) -> ChatCompletion:
response = client.chat.completions.create(
"""
Get an answer from the OpenAI chat model.

Args:
client (OpenAI): The OpenAI client instance.
model (str): The model name to use.
temperature (float): The temperature setting for the model.
messages (List[Dict]): The list of messages to send to the model.
stream (bool, optional): Whether to stream the response. Defaults to False.

Returns:
ChatCompletion: The chat completion response from OpenAI.
"""
return client.chat.completions.create(
model=model, temperature=temperature, messages=messages, stream=stream
)

return response


def get_messages(context: str, query: str, conversation: List[str]) -> List[Dict]:
"""
Prepare the list of messages for the chat model.

Args:
context (str): The context information.
query (str): The user's query.
conversation (List[str]): The conversation history.

Returns:
List[Dict]: The list of messages formatted for the chat model.
"""
return [
{"role": "system", "content": SYSTEM_PROMPT},
{
Expand Down