30 lines
889 B
Python
30 lines
889 B
Python
import os
|
||
from langchain_openai import ChatOpenAI
|
||
|
||
# Configure LLM to connect to local LM Studio server
|
||
llm = ChatOpenAI(
|
||
model="gpt-4o-mini", # or any model name supported by the local server
|
||
temperature=0,
|
||
base_url="http://localhost:1234/v1",
|
||
api_key="lm-studio"
|
||
)
|
||
|
||
def ask(prompt: str) -> str:
|
||
"""Send a prompt to the LLM and return its response."""
|
||
result = llm.invoke({"input": prompt})
|
||
# The output is in result.content
|
||
return result.content
|
||
|
||
if __name__ == "__main__":
|
||
print("Simple AI Agent – type your question (Ctrl+C to exit).")
|
||
while True:
|
||
try:
|
||
user_input = input("\n> ")
|
||
if not user_input.strip():
|
||
continue
|
||
response = ask(user_input)
|
||
print(f"\nAnswer: {response}")
|
||
except (KeyboardInterrupt, EOFError):
|
||
print("\nExiting.")
|
||
break
|