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

OpenLLM: support for system level prompt #31

Merged
merged 2 commits into from
Apr 16, 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
21 changes: 14 additions & 7 deletions llm/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@

openai.api_key = organization.openai_key

prompt = request.data.get("prompt").strip()
question = request.data.get("question").strip()
system_prompt = (
request.data.get("system_prompt", None) or organization.system_prompt
)
system_prompt = system_prompt.strip() if system_prompt else None
logger.info(f"Using the system prompt : {system_prompt}")
Dismissed Show dismissed Hide dismissed

gpt_model = request.data.get("gpt_model", "gpt-3.5-turbo").strip()
session_id = (request.data.get("session_id") or generate_session_id()).strip()

Expand All @@ -59,7 +65,7 @@
messages=[
{
"role": "user",
"content": f"Detect the languages in this text: {prompt}",
"content": f"Detect the languages in this text: {question}",
}
],
functions=[
Expand Down Expand Up @@ -111,7 +117,7 @@

# 2. Pull relevant chunks from vector database
prompt_embeddings = openai.Embedding.create(
model="text-embedding-ada-002", input=prompt
model="text-embedding-ada-002", input=question
)["data"][0]["embedding"]

embedding_results = (
Expand Down Expand Up @@ -146,10 +152,11 @@
# 3. Fetch the chat history from our message store to send to openai and back in the response
historical_chats = Message.objects.filter(session_id=session_id).all()

# 4. Retrievial question and answer (2nd call to OpenAI, use language from 1. to help LLM respond in same language as user question)
# 4. Retrieval question and answer (2nd call to OpenAI, use language from 1. to help LLM respond in same language as user question)
response = openai.ChatCompletion.create(
model=gpt_model,
messages=context_prompt_messages(
system_prompt,
organization.id,
language_results["language"],
relevant_english_context,
Expand All @@ -168,7 +175,7 @@
evaluator_prompts = organization.evaluator_prompts # { 'coherence': ... }
for criteria, evaluator_prompt in evaluator_prompts.items():
score = evaluate_criteria_score(
evaluator_prompt, prompt, prompt_response, gpt_model
evaluator_prompt, question, prompt_response, gpt_model
)
evaluation_scores[criteria] = score
logger.info(f"Evaluated criteria: {criteria} with score: {score}")
Expand All @@ -182,7 +189,7 @@
Message.objects.create(
session_id=session_id,
role="user",
message=prompt,
message=question,
evaluation_score=evaluation_scores,
)
Message.objects.create(
Expand All @@ -195,7 +202,7 @@

return JsonResponse(
{
"question": prompt,
"question": question,
"answer": prompt_response.content,
"language_results": language_results,
"embedding_results_count": len(embedding_results),
Expand Down
4 changes: 2 additions & 2 deletions llm/utils/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


def context_prompt_messages(
system_prompt: str,
organization_id: int,
language: str,
english_context: str,
Expand All @@ -17,10 +18,9 @@ def context_prompt_messages(
) -> list[dict]:
org = Organization.objects.filter(id=organization_id).first()

org_system_prompt = org.system_prompt
examples_text = org.examples_text

system_message_prompt = {"role": "system", "content": org_system_prompt}
system_message_prompt = {"role": "system", "content": system_prompt}
human_message_prompt = {
"role": "user",
"content": f"""Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
Expand Down