Skip to content

Commit

Permalink
feat(langchain): support cohere chat model (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdeichmann authored Nov 20, 2023
1 parent 6466756 commit 4062e30
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions langfuse/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,16 +479,9 @@ def __on_llm_action(
model_name = "anthropic" # unfortunately no model info by anthropic provided.
elif kwargs["invocation_params"]["_type"] == "amazon_bedrock":
# langchain only provides string representation of the model class. Hence have to parse it out.
def extract_model_id(text):
match = re.search(r"model_id='(.*?)'", text)
if match:
return match.group(1)
return None

def extract_second_part(text):
return text.split(".")[-1]

model_name = extract_second_part(extract_model_id(serialized["repr"]))
model_name = self.extract_second_part(self.extract_model_id("model_id", serialized["repr"]))
elif kwargs["invocation_params"]["_type"] == "cohere-chat":
model_name = self.extract_model_id("model", serialized["repr"])
elif kwargs["invocation_params"]["_type"] == "huggingface_hub":
model_name = kwargs["invocation_params"]["repo_id"]
elif kwargs["invocation_params"]["_type"] == "azure-openai-chat":
Expand Down Expand Up @@ -550,6 +543,15 @@ def extract_second_part(text):
except Exception as e:
self.log.exception(e)

def extract_model_id(self, pattern: str, text: str):
match = re.search(rf"{pattern}='(.*?)'", text)
if match:
return match.group(1)
return None

def extract_second_part(selg, text: str):
return text.split(".")[-1]

def on_llm_end(
self,
response: LLMResult,
Expand Down

0 comments on commit 4062e30

Please sign in to comment.