我想使用
langchain-openai
包中的 ChatOpenAI 与 Ubuntu 22 上的本地 LM Studio 服务器进行通信。
我按照文档提出了这个 - 这是标准方法:
from langchain_openai import ChatOpenAI
from dotenv import load_dotenv
load_dotenv()
llm = ChatOpenAI(
api_key=os.getenv("OPENAI_API_KEY"),
base_url=os.getenv("OPENAI_API_BASE_URL"),
temperature=0.5,
)
环境在哪里:
OPENAI_API_BASE_URL="http://localhost:1234/v1"
OPENAI_API_KEY="lm-studio"
但是当我实际使用 llm 时,我收到一个错误:
Traceback (most recent call last):
File "<PROJECT_PATH>/web_researcher/main.py", line 155, in <module>
result = crew.run()
^^^^^^^^^^
File "<PROJECT_PATH>/web_researcher/main.py", line 114, in run
result = crew.kickoff()
^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/langtrace_python_sdk/instrumentation/crewai/patch.py", line 153, in traced_method
result = wrapped(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/crewai/crew.py", line 469, in kickoff
result = self._run_sequential_process()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/crewai/crew.py", line 577, in _run_sequential_process
return self._execute_tasks(self.tasks)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/crewai/crew.py", line 665, in _execute_tasks
task_output = task.execute_sync(
^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/crewai/task.py", line 180, in execute_sync
return self._execute_core(agent, context, tools)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/crewai/task.py", line 234, in _execute_core
result = agent.execute_task(
^^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/langtrace_python_sdk/instrumentation/crewai/patch.py", line 153, in traced_method
result = wrapped(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/crewai/agent.py", line 182, in execute_task
memory = contextual_memory.build_context_for_task(task, context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/crewai/memory/contextual/contextual_memory.py", line 24, in build_context_for_task
context.append(self._fetch_stm_context(query))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/crewai/memory/contextual/contextual_memory.py", line 33, in _fetch_stm_context
stm_results = self.stm.search(query)
^^^^^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/crewai/memory/short_term/short_term_memory.py", line 33, in search
return self.storage.search(query=query, score_threshold=score_threshold)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/crewai/memory/storage/rag_storage.py", line 102, in search
else self.app.search(query, limit)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/embedchain/embedchain.py", line 699, in search
return [{"context": c[0], "metadata": c[1]} for c in self.db.query(**params)]
^^^^^^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/embedchain/vectordb/chroma.py", line 220, in query
result = self.collection.query(
^^^^^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/langtrace_python_sdk/instrumentation/chroma/patch.py", line 91, in traced_method
result = wrapped(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/chromadb/api/models/Collection.py", line 327, in query
valid_query_embeddings = self._embed(input=valid_query_texts)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/chromadb/api/models/Collection.py", line 633, in _embed
return self._embedding_function(input=input)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/chromadb/api/types.py", line 193, in __call__
result = call(self, input)
^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/chromadb/utils/embedding_functions.py", line 188, in __call__
embeddings = self._client.create(
^^^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/langtrace_python_sdk/instrumentation/openai/patch.py", line 467, in traced_method
result = wrapped(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/openai/resources/embeddings.py", line 114, in create
return self._post(
^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/openai/_base_client.py", line 1271, in post
return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/openai/_base_client.py", line 942, in request
return self._request(
^^^^^^^^^^^^^^
File "<COMPUTER_PATH>/.cache/pypoetry/virtualenvs/project-name-977PB1Et-py3.11/lib/python3.11/site-packages/openai/_base_client.py", line 1046, in _request
raise self._make_status_error_from_response(err.response) from None
openai.AuthenticationError: Error code: 401 - {'error': {'message': 'Incorrect API key provided: lm-studio. You can find your API key at https://platform.openai.com/account/api-keys.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_api_key'}}
Ubuntu 22
版本:
Python:3.11.5
套餐:
也许我做错了什么,但我不这么认为,我阅读了很多文档和网络帖子,所有这些都表明这是正确的方法。
您的 openai API 密钥似乎存在问题