Created
September 21, 2025 11:15
-
-
Save mahenzon/faf5b8f17c7e3f29ede4057880e2d1c2 to your computer and use it in GitHub Desktop.
Python AI LLM example using langchain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 3.13 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| API_KEY = "EMPTY" | |
| BASE_URL = "http://localhost:12345/engines/llama.cpp/v1" | |
| URL = f"{BASE_URL}/chat/completions" | |
| # MODEL = "ai/gemma3" | |
| MODEL = "ai/gemma3n" | |
| headers = { | |
| "Authorization": f"Bearer {API_KEY}", | |
| } | |
| def main(): | |
| system_message = { | |
| "role": "system", | |
| "content": ( | |
| "You are a helpful assistant. " | |
| "Answer questions clearly an concisely. " | |
| "If you don't know the answer, you have to say so. " | |
| "Otherwise provide concise answer, don't add any extra info that user didn't ask for." | |
| ), | |
| } | |
| user_message = { | |
| "role": "user", | |
| # "content": "Hello, who are you?", | |
| # "content": "What is the capital of Russia?", | |
| # "content": "What is the capital of Russia? Also give one the most important fact about the capital.", | |
| "content": ( | |
| "Context: square is red, triangle is yellow, hexagon is blue, circle is green\n" | |
| "Question: what color is hex?" | |
| # "Question: what color is circle?" | |
| ), | |
| } | |
| data = { | |
| "model": MODEL, | |
| "messages": [ | |
| system_message, | |
| user_message, | |
| ], | |
| } | |
| response = requests.post(URL, json=data, headers=headers) | |
| if response.ok: | |
| # pprint(response.json()) | |
| print(response.json()["choices"][0]["message"]["content"]) | |
| else: | |
| print(response) | |
| print(response.text) | |
| if __name__ == "__main__": | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [project] | |
| name = "075-ai-llm-how-to" | |
| version = "0.1.0" | |
| description = "Add your description here" | |
| readme = "README.md" | |
| requires-python = ">=3.13" | |
| dependencies = [ | |
| "langchain>=0.3.27", | |
| "langchain-openai>=0.3.33", | |
| "notebook>=7.4.5", | |
| "requests>=2.32.5", | |
| ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment