SyedHasanCronosPMC commited on
Commit
eae3210
·
verified ·
1 Parent(s): a5385c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -6
app.py CHANGED
@@ -4,7 +4,6 @@ import matplotlib.pyplot as plt
4
  import pandas as pd
5
  from langgraph.graph import StateGraph
6
  from langchain_core.messages import HumanMessage, AIMessage
7
- from langchain_anthropic import ChatAnthropic
8
  import warnings
9
  warnings.filterwarnings("ignore")
10
 
@@ -20,11 +19,51 @@ api_key = os.getenv("ANTHROPIC_API_KEY")
20
  if not api_key:
21
  raise ValueError("ANTHROPIC_API_KEY environment variable not set")
22
 
23
- # Fix: Initialize ChatAnthropic correctly without any proxy settings
24
- llm = ChatAnthropic(
25
- anthropic_api_key=api_key, # Changed from api_key to anthropic_api_key
26
- model="claude-3-5-sonnet-20240229"
27
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  # System prompt constructor
30
  def make_system_prompt(suffix: str) -> str:
 
4
  import pandas as pd
5
  from langgraph.graph import StateGraph
6
  from langchain_core.messages import HumanMessage, AIMessage
 
7
  import warnings
8
  warnings.filterwarnings("ignore")
9
 
 
19
  if not api_key:
20
  raise ValueError("ANTHROPIC_API_KEY environment variable not set")
21
 
22
+ # Create a custom LLM implementation to avoid the proxies issue
23
+ def create_llm():
24
+ # Directly use the Anthropic client instead of LangChain's wrapper
25
+ from anthropic import Anthropic
26
+
27
+ # Create the base client without any proxies
28
+ client = Anthropic(api_key=api_key)
29
+
30
+ # Create a simple wrapper function that mimics the LangChain interface
31
+ class CustomAnthropicLLM:
32
+ def __init__(self, client, model):
33
+ self.client = client
34
+ self.model = model
35
+
36
+ def invoke(self, inputs):
37
+ if isinstance(inputs, dict) and "messages" in inputs:
38
+ messages = inputs["messages"]
39
+ formatted_messages = []
40
+
41
+ for msg in messages:
42
+ role = "user" if isinstance(msg, HumanMessage) else "assistant"
43
+ formatted_messages.append({"role": role, "content": msg.content})
44
+
45
+ response = self.client.messages.create(
46
+ model=self.model,
47
+ messages=formatted_messages,
48
+ max_tokens=1024
49
+ )
50
+ return response.content[0].text
51
+
52
+ elif isinstance(inputs, str):
53
+ response = self.client.messages.create(
54
+ model=self.model,
55
+ messages=[{"role": "user", "content": inputs}],
56
+ max_tokens=1024
57
+ )
58
+ return response.content[0].text
59
+
60
+ else:
61
+ raise ValueError(f"Unsupported input format: {type(inputs)}")
62
+
63
+ return CustomAnthropicLLM(client, "claude-3-5-sonnet-20240229")
64
+
65
+ # Create our custom LLM
66
+ llm = create_llm()
67
 
68
  # System prompt constructor
69
  def make_system_prompt(suffix: str) -> str: