Skip to content

Can't connect with LM Studio (0.1.14 still defaulting to OpenAI API, 401 error) #38

@DannyVee-stack

Description

@DannyVee-stack

Great project! I'm really excited to see where this goes.

I want to be able to connect with LM Studio's APIs, so I put this script together quickly

local_model_api_tool.py

`from langchain.tools import tool
from openai import OpenAI

Define the tool
@tool
def local_model_api_tool(input_content: str) -> str:
    """
    This tool interfaces with a local model API using OpenAI client.
    The input is a string, which is sent to the API, and the response is returned.
    """
    # Configure the OpenAI client to use the local server
    client = OpenAI(base_url="http://localhost:8000/v1", api_key="NULL")

    # Sending the request to the local model
    response = client.chat.completions.create(
        model="local-model",  # Model field (unused in this setup)
        messages=[
            {"role": "system", "content": "Always answer in rhymes."},
            {"role": "user", "content": input_content}
        ],
        temperature=0.7,
    )

    # Extracting the message from the response
    return response.choices[0].message
`

Then put it in as a tool for the agent:

`import os
from crewai import Agent, Task, Crew, Process
from local_model_api_tool import local_model_api_tool  # Import the custom tool

os.environ["OPENAI_API_KEY"] = "NULL"

# Define your agents with roles and goals
researcher = Agent(
    role='Researcher',
    goal='Discover new insights',
    backstory="You're a world class researcher working on a major data science company",
    verbose=True,
    allow_delegation=False,
    tools=[local_model_api_tool]  #Tool assignment`
Still getting the 401 error though:

openai.AuthenticationError: Error code: 401 - {'error': {'message': 'Incorrect API key provided: NULL. You can find your API key at https://platform.openai.com/account/api-keys.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_api_key'}}


Any ideas on how to fix this? It'd be great to be able to to test many different models through LM Studio, and even assign a specialized model to different agents. 

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationgood first issueGood for newcomershelp wantedExtra attention is neededquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions