使用langchain支持openai,如何支持自定义api_key和base_url ?

langchain目前还使用的是旧版的openai的接口,需要注意

示例代码,使用LLM进行预测

核心其实在于key和url的设置

方法有:

1、使用环境变量来设置   
2、使用变量来传入   
3、使用手动设置环境变量  

环境变量可以这样设置:

echo export OPENAI_API_KEY="sk-xxxxx" > ~/.bashrc
source ~/.bashrc

先安装

pip3 install langchain
pip3 install opneai

直接上示例代码:

1、先从这里拿到账号:https://github.com/xing61/xiaoyi-robot

2、代码:

import os
import requests
import time
import json
import time
from langchain.llms import OpenAI

API_SECRET_KEY = "你在智增增的key";
BASE_URL = "https://flag.smarttrot.com/v1"; #智增增的base-url

os.environ["OPENAI_API_KEY"] = API_SECRET_KEY
os.environ["OPENAI_API_BASE"] = BASE_URL

def text():
    llm = OpenAI(temperature=0.9)
    text = "What would be a good company name for a company that makes colorful socks?"
    print(llm(text))

if __name__ == '__main__':
    text();

可以从后台看到langchain是怎么调用智增增接口的:

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值