设置langchain_openai代理proxyman抓包
环境macos 14.6.1、openai 1.44.1,langchain 0.2.16,langchain-openai 0.1.22
,proxyman代理本地端口9090
,设置langchain_openai代理proxyman抓包,正常写法,传参http_client
配置verify=False
、proxies
允许本地证书抓包。
import os
os.environ["OPENAI_API_KEY"]="sk-xxxx"
os.environ["OPENAI_API_BASE"]="https://api.gptapi.us/v1"
from pprint import pprint
from langchain_core.messages import AIMessage, HumanMessage
messages = [AIMessage(content=f"So you said you were researching ocean mammals?", name="Model")]
messages.append(HumanMessage(content=f"Yes, that's right.",name="Lance"))
messages.append(AIMessage(content=f"Great, what would you like to learn about.", name="Model"))
messages.append(HumanMessage(content=f"I want to learn about the best place to see Orcas in the US.", name="Lance"))
for m in messages:
m.pretty_print()
# Create a custom httpx client with SSL verification disabled
from httpx import Client
proxies = {
"http://": "http://192.168.8.110:9090",
"https://": "http://192.168.8.110:9090"
}
custom_client = Client(verify=False, proxies=proxies)
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o", http_client=custom_client)
result = llm.invoke(messages)
print("Result:", result, type(result))
# openai包同理
from openai import OpenAI
client = OpenAI(
# This is the default and can be omitted
api_key=os.environ.get("OPENAI_API_KEY"),
base_url=os.environ.get("OPENAI_API_BASE"),
http_client=http_client,
)
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": "Say this is a test",
}
],
model="gpt-3.5-turbo",
)
print(chat_completion.choices[0].message.content)
野路子,直接改langchain_openai代码,通过环境变量OPENAI_PROXY
有值时,设置verify=False
允许所有openai请求抓包,下次只需要设置OPENAI_PROXY
环境变量就可以了
# /Users/xxx/miniforge3/envs/lc-academy-env/lib/python3.11/site-packages/langchain_openai/chat_models/base.py (505-517)
if not self.client:
if self.openai_proxy and not self.http_client:
try:
import httpx
except ImportError as e:
raise ImportError(
"Could not import httpx python package. "
"Please install it with `pip install httpx`."
) from e
- self.http_client = httpx.Client(proxy=self.openai_proxy)
+ self.http_client = httpx.Client(proxy=self.openai_proxy, verify=False)
sync_specific = {"http_client": self.http_client}
self.root_client = openai.OpenAI(**client_params, **sync_specific) # type: ignore[arg-type]
self.client = self.root_client.chat.completions
调用示例
import os
os.environ["OPENAI_API_KEY"]="sk-xxxx"
os.environ["OPENAI_API_BASE"]="https://api.gptapi.us/v1"
os.environ["OPENAI_PROXY"]="http://192.168.8.110:9090"
from pprint import pprint
from langchain_core.messages import AIMessage, HumanMessage
messages = [AIMessage(content=f"So you said you were researching ocean mammals?", name="Model")]
messages.append(HumanMessage(content=f"Yes, that's right.",name="Lance"))
messages.append(AIMessage(content=f"Great, what would you like to learn about.", name="Model"))
messages.append(HumanMessage(content=f"I want to learn about the best place to see Orcas in the US.", name="Lance"))
for m in messages:
m.pretty_print()
# Create a custom httpx client with SSL verification disabled
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o")
result = llm.invoke(messages)
print("Result:", result, type(result))