真正适合spring ai的使用方式

上周被spring-ai调用模型api这个折腾的有点难受,这周又试了下用deepseek官方的api,但结果还是一样,不确定是啥原因,请求会一直卡着
后来想着本地有ollama,那是不是可以直接用ollama来调本地模型
这里我们用一个非常基础很小的模型

1
ollama run gemma3:1b

然后对应的配置是

1
2
3
4
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>

当然这里有个坑,因为目前还不成熟,所以这个包的依赖跟之前openai的是不兼容的
需要引用

1
2
3
4
5
6
7
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.0-M1</version>
<type>pom</type>
<scope>import</scope>
</dependency>

属性配置就很简单了

1
2
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.model=gemma3:1b

然后就是简单写个Controller就可以了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RestController
@RequestMapping("/demo")
public class OllamaController {
private static final Logger logger = LoggerFactory.getLogger(OllamaController.class);

@Autowired
private OllamaChatModel ollamaChatModel;


@RequestMapping("/ollama")
public Object ollama(String msg) {
String call = ollamaChatModel.call(msg);
return call;
}
}

这样我们的初始调用就完成了
比如我们提问 ‘how to learn java’

1
Okay, let's break down how to learn Java! It can seem daunting at first, but with a structured approach, you'll be coding in Java in no time. Here’s a breakdown of a recommended learning path, broken down into phases: **1. Foundation (2-4 Weeks)** * **Understand the Basics:** * **What is Java?** Java is a robust, object-oriented programming language known for its platform independence ("write once, run anywhere"). It’s used for enterprise applications, Android development, and more. * **Why Learn Java?** It’s a popular choice for beginners because it’s relatively easy to learn, has a large community, and is used in many real-world applications. * **Key Concepts:** * **Variables & Data Types:** Understand different data types (int, float, String, boolean, etc.). * **Operators:** Arithmetic, comparison, logical, assignment. * **Control Flow:** `if`, `else`, `for`, `while` – controlling the flow of your program. * **Basic Syntax:** The code structure of Java. * **Resources:** * **Tutorialspoint Java Course:** [https://www.tutorialspoint.com/java/index.htm](https://www.tutorialspoint.com/java/index.htm) - A good, comprehensive, and free overview. * **Codecademy's Learn Java:** [https://www.codecademy.com/learn/learn-java](https://www.codecademy.com/learn/learn-java) - Interactive, hands-on exercises. * **Google's Java Tutorial:** [https://developer.android.com/training/jdbc](https://developer.android.com/training/jdbc) (Java is built on JDBC, so this will give you a baseline) **2. Core Concepts (4-8 Weeks)** * **Data Structures:** * **Arrays:** Fundamental data structures. Understand how to declare, initialize, and use arrays. * **Linked Lists:** Another essential data structure, offering flexibility. * **Object-Oriented Programming (OOP) Fundamentals:** * **Classes and Objects:** The basis of OOP. Learn how to define classes, create objects, and interact with them. * **Encapsulation:** Bundling data and methods that operate on that data within a class. * **Inheritance:** Creating new classes based on existing classes. * **Polymorphism:** The ability of objects to take on many forms. * **Java Syntax & Semantics:** * **Statements:** The commands you write. * **Expressions:** Results of calculations. * **Keywords:** Reserved words with special meanings (e.g., `class`, `if`, `else`). * **Comments:** Explain your code. * **Resources:** * **Oracle Java Tutorials:** [https://docs.oracle.com/javase/tutorial/](https://docs.oracle.com/javase/tutorial/) – A more in-depth guide. * **FreeCodeCamp's Java Tutorial:** [https://www.freecodecamp.org/learn/java-tutorial/](https://www.freecodecamp.org/learn/java-tutorial/) - Great for visual learners. **3. Practical Application - Projects** * **Simple Programs:** Start with small, manageable projects: * **Number Guessing Game:** A classic introductory project. * **Rock-Paper-Scissors:** A basic example of OOP. * **Simple Calculator:** Reinforces fundamental concepts. * **Gradually Increase Complexity:** * **Print Statements:** A simple way to understand output. * **Palindrome Checker:** A challenge to practice string manipulation. * **Basic Console Applications:** Write programs that run directly in the console. **4. Intermediate Concepts (Ongoing)** * **Exception Handling:** Understanding and handling errors. * **Collections:** Learn about ArrayLists, LinkedLists, Sets, and Maps. * **String Manipulation:** Working with strings effectively. * **Multithreading:** Understanding how to run multiple tasks concurrently (basic concepts). * **Java Collections Framework:** The cornerstone of modern Java programming. **Resources for Continued Learning:** * **Stack Overflow:** [https://stackoverflow.com/](https://stackoverflow.com/) - An invaluable resource for solving coding problems and learning from others. * **Reddit's r/java:** [https://www.reddit.com/r/java/](https://www.reddit.com/r/java/) - A community forum with discussions and resources. * **YouTube:** Search for Java tutorials and courses (e.g., "Java for beginners"). **Tips for Success:** * **Practice Regularly:** Even 30 minutes a day is better than long, infrequent sessions. * **Write Code Frequently:** The more you write, the better you'll understand. * **Don't Be Afraid to Make Mistakes:** Mistakes are part of the learning process. Learn from them. * **Read Code:** Look at code written by others to understand different approaches. * **Join a Community:** Connecting with others can provide support and motivation. **To help me tailor my advice even more, could you tell me:** * **What's your background in programming?** (e.g., Have you programmed before? If so, what languages?) * **What are your goals for learning Java?** (e.g., Career change? Personal project? Specific application like Android?)

格式不太美观,但是至少是能输出来结果了,而且可以选择本地可用的最佳模型,话说openai的oss试了下,本地有点吃不消,不然可能是比较合适的