Skip to content

Commit

Permalink
feat: upgrade to claude 3 and gpt 4o
Browse files Browse the repository at this point in the history
  • Loading branch information
nsantacruz committed May 21, 2024
1 parent 33d22d8 commit c7a0077
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/basic_langchain/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __call__(self, messages: List[AbstractMessage]) -> AIMessage:
# claude wants system messages as a kwarg
system = messages[0].content
messages.pop(0)
response = self.client.beta.messages.create(
response = self.client.messages.create(
model=self.model,
system=system,
temperature=self.temperature,
Expand Down
2 changes: 1 addition & 1 deletion app/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
langchain[llms]==0.1.2
anthropic~=0.15.1
anthropic~=0.25.1
stanza~=1.5.0
openai~=1.12.0
typer~=0.4.1
Expand Down
2 changes: 1 addition & 1 deletion app/summarize_commentary/summarize_commentary.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def summarize_commentary(source: TopicPromptSource, topic: Topic, company='opena
print(f"Number of commentary tokens: {num_tokens}")
completion = get_completion_openai(prompt)
elif company == 'anthropic':
llm = ChatAnthropic(model="claude-instant-1.2", temperature=0)
llm = ChatAnthropic(model="claude-3-haiku-20240307", temperature=0)
completion = llm([HumanMessage(content=prompt)]).content
else:
raise Exception("No valid company passed. Options are 'openai' or 'anthropic'.")
Expand Down
2 changes: 1 addition & 1 deletion app/topic_modelling/poc.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def get_raw_topics(text, lang):
user_prompt = PromptTemplate.from_template("# Input\n{text}")
human_message = HumanMessage(content=user_prompt.format(text=text))

# llm = ChatOpenAI(model="gpt-4", temperature=0)
# llm = ChatOpenAI(model="gpt-4o", temperature=0)
llm = ChatAnthropic(model="claude-2", temperature=0)

response = llm([system_message, human_message])
Expand Down
4 changes: 2 additions & 2 deletions app/topic_prompt/contextualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def context_from_surrounding_text(source: TopicPromptSource) -> str:
human_message = HumanMessage(content=f"<segment>{segment_text}</segment>\n"
f"<context>{section_text}</context>\n"
f"<hint>{source.context_hint}</hint>")
llm = ChatAnthropic(model="claude-2.1", temperature=0)
llm = ChatAnthropic(model="gpt-4o", temperature=0)
response = llm([system_message, human_message])
context = get_by_xml_tag(response.content, "relevant_context").strip()
if context is None:
Expand All @@ -44,7 +44,7 @@ def context_from_surrounding_text(source: TopicPromptSource) -> str:

def general_context(source: TopicPromptSource):
text = get_source_text_with_fallback(source, "en", auto_translate=True)
llm = ChatOpenAI(model="gpt-4", temperature=0)
llm = ChatOpenAI(model="gpt-4o", temperature=0)
system_message = SystemMessage(content=f"""
Given a text from the Jewish cannon, add any relevant context that would help a user understand this text from a
Jewish perspective. Relevant context may be:
Expand Down
6 changes: 3 additions & 3 deletions app/topic_prompt/differentiate_writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


def differentiate_writing(sentence, phrase_to_avoid):
llm = ChatOpenAI(model="gpt-4", temperature=0)
llm = ChatOpenAI(model="gpt-4o", temperature=0)
system_message = SystemMessage(content="Given a target sentence (wrapped in <target> tags) and a phrase to avoid "
"using (wrapped in <phrase_to_avoid> tags), rewrite <target> so it doesn't "
"use <phrase_to_avoid> but maintains the same meaning, tone and terminology."
Expand All @@ -30,7 +30,7 @@ def differentiate_writing(sentence, phrase_to_avoid):


def remove_dependent_clause(sentence, phrase_to_avoid):
llm = ChatOpenAI(model="gpt-4", temperature=0)
llm = ChatOpenAI(model="gpt-4o", temperature=0)
system_message = SystemMessage(content="Given a target sentence (wrapped in <target> tags) and a phrase to avoid "
"using (wrapped in <phrase_to_avoid> tags):\n"
"1) Determine if <phrase_to_avoid> includes an explanation of a term\n"
Expand All @@ -55,7 +55,7 @@ def remove_dependent_clause(sentence, phrase_to_avoid):


def repeated_phrase(sentence, comparison_sentences: list[str]):
llm = ChatOpenAI(model="gpt-4", temperature=0)
llm = ChatOpenAI(model="gpt-4o", temperature=0)
system_message = SystemMessage(content="Given a target sentence (wrapped in <target> tags) and a list of "
"comparison sentences (each wrapped in <comparison> tags):\n"
"1) Decide if there is a phrase shared between the target sentence and at least one comparison sentence\n"
Expand Down
4 changes: 2 additions & 2 deletions app/topic_prompt/topic_prompt_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def _get_toprompt_options(lang: str, topic: Topic, source: TopicPromptSource, ot
num_tries=1, phrase_to_avoid=None) -> TopromptOptions:
# TODO pull out formatting from _get_input_prompt_details
llm_prompt = TopromptLLMPrompt(lang, topic, source, other_sources).get()
llm = ChatOpenAI(model="gpt-4", temperature=0)
llm = ChatOpenAI(model="gpt-4o", temperature=0)
human_message = HumanMessage(content=llm_prompt.format())
responses = []
topic_prompts = []
Expand Down Expand Up @@ -67,7 +67,7 @@ def _improve_title(curr_responses, curr_title):
f"Rewrite the title, rephrasing to avoid using a colon."
f" Wrap the title in <title> tags. It should at most"
f" five words and grab the reader's attention.")
llm = ChatOpenAI(model="gpt-4", temperature=0.5)
llm = ChatOpenAI(model="gpt-4o", temperature=0.5)
title_response = llm(curr_responses + [HumanMessage(content=better_title_prompt.format())])
title_match = re.search(r'<title>(.+?)</title>', title_response.content)
if title_match is None:
Expand Down
4 changes: 2 additions & 2 deletions app/topic_prompt/uniqueness_of_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get_uniqueness_of_source(source: TopicPromptSource, topic: Topic, other_sour

def summarize_based_on_uniqueness(text: str, uniqueness: str) -> str:

llm = ChatOpenAI(model="gpt-4", temperature=0)
llm = ChatOpenAI(model="gpt-4o", temperature=0)
system_message = SystemMessage(content=
"You are an intelligent Jewish scholar who is knowledgeable in all aspects of the Torah and Jewish texts.\n"
"# Task\n"
Expand Down Expand Up @@ -90,7 +90,7 @@ def _get_uniqueness_of_source_as_compared_to_other_sources(source: TopicPromptSo
'"hint": "{hint}"'
'}}}}')
human_message = HumanMessage(content=prompt.format(**prompt_inputs))
llm = ChatOpenAI(model="gpt-4", temperature=0)
llm = ChatOpenAI(model="gpt-4o", temperature=0)

response = llm([system_message, human_message])
uniqueness = re.sub(fr'^"?{uniqueness_preamble}\s*', '', response.content)
Expand Down
4 changes: 2 additions & 2 deletions app/util/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import tiktoken


def count_tokens_openai(prompt, model="gpt-4"):
def count_tokens_openai(prompt, model="gpt-4o"):
encoding = tiktoken.encoding_for_model(model)
num_tokens = len(encoding.encode(prompt))
return num_tokens


def get_completion_openai(prompt, model="gpt-4"):
def get_completion_openai(prompt, model="gpt-4o"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
Expand Down

0 comments on commit c7a0077

Please sign in to comment.