Package: think-llm
ThinkThink is a Python package for creating thinking programs.It provides simple but powerful primitives forrobustintegration of Large Language
Models (LLMs) into your Python programs.ExamplesUsing AI as an ordinary function:fromthink.llm.openaiimportChatGPTfromthink.aiimportai@aidefhaiku(topic:str)->str:"""Write a haiku about {{ topic }}"""llm=ChatGPT()print(haiku(llm,topic="computers"))Allowing AI to use tools:fromdatetimeimportdatefromthink.llm.openaiimportChatGPTfromthink.chatimportChatfromthink.toolimporttool@tooldefcurrent_date()->str:"""Get the current date.:returns: current date in YYYY-MM-DD format"""returndate.today().isoformat()llm=ChatGPT()chat=Chat("You are a helpful assistant.")chat.user("How old are you (in days since your knowledge cutoff)?")print(llm(chat,tools=[current_date]))Parsing AI output:importjsonfrompydanticimportBaseModelfromthink.llm.openaiimportChatGPTfromthink.chatimportChatfromthink.parserimportJSONParserclassCityInfo(BaseModel):name:strcountry:strpopulation:intlatitude:floatlongitude:floatllm=ChatGPT()parser=JSONParser(spec=CityInfo)chat=Chat("You are a hepful assistant. Your task is to answer questions about cities, ""to the best of your knowledge. Your output must be a valid JSON conforming to ""this JSON schema:\n"+json.dumps(parser.schema)).user(city)answer=llm(chat,parser=parser)print(f"{answer.name}is a city in{answer.country}with{answer.population}inhabitants.")print(f"It is located at{answer.latitude}latitude and{answer.longitude}longitude.")QuickstartInstall viapip:pipinstallthink-llmNote that the package name isthink-llm,notthink.Set up your LLM credentials (OpenAI or Anthropic, depending on the LLM you want to use):exportOPENAI_API_KEY=<your-openai-key>exportANTHROPIC_API_KEY=<your-anthropic-key>And you're ready to go:fromthink.llm.openaiimportChatGPTfromthink.chatimportChatllm=ChatGPT()chat=Chat("You are a helpful assistant.").user("Tell me a funny joke.")print(llm(chat))Explore theexamplesdirectory for more usage examples, and the
source code for documentation on how to use the library (until we build proper docs! if you
want to help out with that, please see below).RoadmapFeatures and capabilities that are planned for the near future:documentationfull support for Anthropic (tools, parsers, AI functions)support for other LLM APIs via LiteLLM or similarsupport for local LLMs via HuggingFacemore examplesIf you want to help with any of these, please look at the open issues, join the
conversation and submit a PR. Please read the Contributing section below.ContributingContributions are welcome!To ensure that your contribution is accepted, please follow these guidelines:open an issue to discuss your idea before you start working on it, or if there's
already an issue for your idea, join the conversation there and explain how you
plan to implement itmake sure that your code is well documented (docstrings, type annotations, comments,
etc.) and tested (test coverage should only go up)make sure that your code is formatted and type-checked withruff(default settings)CopyrightCopyright (C) 2023-2024. Senko Rasic and Think contributors. You may use and/or distribute
this project under the terms of MIT license. See the LICENSE file for more details.